2023-09-09 04:00:00 +02:00
|
|
|
package rebound
|
|
|
|
|
|
|
|
import (
|
2023-09-24 20:21:44 +02:00
|
|
|
"log/slog"
|
2023-09-09 04:00:00 +02:00
|
|
|
"net"
|
2023-09-24 20:21:44 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
"forge.cadoles.com/wpetit/rebound/http"
|
|
|
|
"forge.cadoles.com/wpetit/rebound/ssh"
|
2023-09-24 20:21:44 +02:00
|
|
|
"forge.cadoles.com/wpetit/rebound/stat"
|
2023-09-09 04:00:00 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-09-21 05:54:08 +02:00
|
|
|
listener net.Listener
|
|
|
|
opts *Options
|
2023-09-24 20:21:44 +02:00
|
|
|
stats *stat.Store
|
2023-09-09 04:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
func (s *Server) Start() error {
|
|
|
|
s.log("[INFO] listening on %s", s.opts.Address)
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-24 20:21:44 +02:00
|
|
|
if err := s.stats.Load(s.opts.StatsFile); err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
s.log("[INFO] stats file does not exist. ignoring.")
|
|
|
|
} else {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
listener, err := net.Listen("tcp", s.opts.Address)
|
|
|
|
if err != nil {
|
2023-09-09 04:00:00 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
s.listener = listener
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
sshListener, httpListener := s.muxListener(listener)
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
go func() {
|
|
|
|
defer listener.Close()
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
server := ssh.NewServer(
|
|
|
|
ssh.WithHostKey(s.opts.SSH.HostKey),
|
|
|
|
ssh.WithPublicHost(s.opts.SSH.PublicHost),
|
|
|
|
ssh.WithPublicPort(s.opts.SSH.PublicPort),
|
|
|
|
ssh.WithSockDir(s.opts.SSH.SockDir),
|
|
|
|
ssh.WithLogger(s.opts.SSH.Logger),
|
2023-09-24 20:21:44 +02:00
|
|
|
ssh.WithStats(s.stats),
|
2023-09-21 05:54:08 +02:00
|
|
|
)
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
if err := server.Serve(sshListener); err != nil {
|
|
|
|
s.log("[ERROR] %+v", errors.WithStack(err))
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}()
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
go func() {
|
|
|
|
defer listener.Close()
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
server := http.NewServer(
|
|
|
|
http.WithCustomDir(s.opts.HTTP.CustomDir),
|
2023-09-24 17:33:20 +02:00
|
|
|
http.WithTemplateData(s.opts.HTTP.TemplateData),
|
2023-09-21 05:54:08 +02:00
|
|
|
http.WithLogger(s.opts.HTTP.Logger),
|
2023-09-24 20:21:44 +02:00
|
|
|
http.WithStats(s.stats),
|
2023-09-21 05:54:08 +02:00
|
|
|
)
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
if err := server.Serve(httpListener); err != nil {
|
|
|
|
s.log("[ERROR] %+v", errors.WithStack(err))
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
2023-09-09 04:00:00 +02:00
|
|
|
|
2023-09-24 20:21:44 +02:00
|
|
|
go func() {
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(s.opts.StatsFileSaveInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
<-ticker.C
|
|
|
|
|
|
|
|
slog.Info("saving stats", slog.String("file", s.opts.StatsFile), slog.Duration("interval", s.opts.StatsFileSaveInterval))
|
|
|
|
if err := s.stats.Save(s.opts.StatsFile); err != nil {
|
|
|
|
slog.Error("could not save stat file", slog.Any("error", errors.WithStack(err)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-09-09 04:00:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
func (s *Server) Stop() error {
|
|
|
|
if s.listener == nil {
|
|
|
|
return nil
|
2023-09-09 04:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
if err := s.listener.Close(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-09-09 04:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:54:08 +02:00
|
|
|
s.listener = nil
|
|
|
|
|
|
|
|
return nil
|
2023-09-09 04:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) log(message string, args ...any) {
|
|
|
|
s.opts.Logger(message, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(funcs ...OptionFunc) *Server {
|
|
|
|
opts := DefaultOptions()
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
2023-09-24 20:21:44 +02:00
|
|
|
opts: opts,
|
|
|
|
stats: stat.NewStore(),
|
2023-09-09 04:00:00 +02:00
|
|
|
}
|
|
|
|
}
|