diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f636386..2802be9 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -57,6 +57,7 @@ nfpms: packager: deb - src: misc/packaging/systemd/rebound.env dst: /etc/rebound/environ + type: config|noreplace packager: deb # RPM @@ -64,6 +65,7 @@ nfpms: dst: /usr/lib/systemd/system/rebound.service packager: rpm - src: misc/packaging/systemd/rebound.env + type: config|noreplace dst: /etc/rebound/environ packager: rpm @@ -74,6 +76,7 @@ nfpms: mode: 0755 packager: apk - src: misc/packaging/openrc/rebound.conf + type: config|noreplace dst: /etc/conf.d/rebound file_info: mode: 0755 @@ -84,14 +87,6 @@ nfpms: type: dir file_info: mode: 0700 - - dst: /etc/rebound/custom - type: dir - file_info: - mode: 0700 - - dst: /usr/share/rebound - type: dir - file_info: - mode: 0700 - dst: /var/log/rebound type: dir file_info: diff --git a/cmd/server/main.go b/cmd/server/main.go index 285ea0a..4631d77 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,7 +1,6 @@ package main import ( - "flag" "log" "os" "os/signal" @@ -19,30 +18,17 @@ func main() { log.Fatalf("[ERROR] %+v", errors.WithStack(err)) } - // Global Options - address := flag.String("address", opts.Address, "server listening address") - - // SSH Options - sockDir := flag.String("ssh-sock-dir", opts.SSH.SockDir, "ssh sock directory") - publicPort := flag.Uint("ssh-public-port", opts.SSH.PublicPort, "ssh public port") - publicHost := flag.String("ssh-public-host", opts.SSH.PublicHost, "ssh public host") - hostKey := flag.String("ssh-host-key", opts.SSH.HostKey, "ssh host key") - - // HTTP Options - customDir := flag.String("http-custom-dir", opts.HTTP.CustomDir, "http custom templates/assets directory") - - flag.Parse() - server := rebound.NewServer( - rebound.WithAddress(*address), + rebound.WithAddress(opts.Address), rebound.WithSSHOption( - ssh.WithSockDir(*sockDir), - ssh.WithPublicHost(*publicHost), - ssh.WithPublicPort(*publicPort), - ssh.WithHostKey(*hostKey), + ssh.WithSockDir(opts.SSH.SockDir), + ssh.WithPublicHost(opts.SSH.PublicHost), + ssh.WithPublicPort(opts.SSH.PublicPort), + ssh.WithHostKey(opts.SSH.HostKey), ), rebound.WitHTTPOption( - http.WithCustomDir(*customDir), + http.WithCustomDir(opts.HTTP.CustomDir), + http.WithTemplateData(opts.HTTP.TemplateData), ), ) diff --git a/http/options.go b/http/options.go index dd85939..b09cc1f 100644 --- a/http/options.go +++ b/http/options.go @@ -1,10 +1,19 @@ package http -import "log" +import ( + "log" +) type Options struct { - Logger func(message string, args ...any) - CustomDir string `env:"CUSTOM_DIR"` + Logger func(message string, args ...any) + CustomDir string `env:"CUSTOM_DIR"` + TemplateData *TemplateData `envPrefix:"TEMPLATE_DATA_"` +} + +type TemplateData struct { + Title string `env:"TITLE"` + SSHPublicHost string `env:"SSH_PUBLIC_HOST"` + SSHPublicPort int `env:"SSH_PUBLIC_PORT"` } type OptionFunc func(*Options) @@ -13,6 +22,11 @@ func DefaultOptions() *Options { return &Options{ Logger: log.Printf, CustomDir: "", + TemplateData: &TemplateData{ + Title: "Rebound", + SSHPublicHost: "127.0.0.1", + SSHPublicPort: 2222, + }, } } @@ -27,3 +41,9 @@ func WithCustomDir(customDir string) func(*Options) { opts.CustomDir = customDir } } + +func WithTemplateData(templateData *TemplateData) func(*Options) { + return func(opts *Options) { + opts.TemplateData = templateData + } +} diff --git a/http/server.go b/http/server.go index c61094d..9708cc8 100644 --- a/http/server.go +++ b/http/server.go @@ -30,13 +30,7 @@ type Server struct { } func (s *Server) serveHomepage(w http.ResponseWriter, r *http.Request) { - data := struct { - Title string - }{ - Title: "Rebound", - } - - s.renderTemplate(w, "index", data) + s.renderTemplate(w, "index", s.opts.TemplateData) } func (s *Server) Serve(l net.Listener) error { diff --git a/http/templates/footer.html b/http/templates/footer.html index 3ee490e..6ea7bbd 100644 --- a/http/templates/footer.html +++ b/http/templates/footer.html @@ -2,7 +2,7 @@ diff --git a/http/templates/index.html b/http/templates/index.html index 622f77c..835c702 100644 --- a/http/templates/index.html +++ b/http/templates/index.html @@ -17,7 +17,7 @@
Rebound est un serveur SSH permettant de créer des tunnels TCP/IP éphémères et privés entre 2 machines positionnées derrière un NAT.
Pour l'utiliser un simple client SSH suffit !
-ssh -R 0:127.0.0.1:<port> rebound@rebound.cadol.es
+ ssh -R 0:127.0.0.1:<port> rebound@{{ .SSHPublicHost }} -p {{ .SSHPublicPort }}
Où <port> est à remplacer par le port du service s'exécutant sur votre machine en local.
Une fois connecté, suivez les instructions. 😉
diff --git a/misc/packaging/openrc/rebound.conf b/misc/packaging/openrc/rebound.conf index ed7004a..dd658af 100644 --- a/misc/packaging/openrc/rebound.conf +++ b/misc/packaging/openrc/rebound.conf @@ -3,4 +3,7 @@ export REBOUND_HTTP_CUSTOM_DIR=/etc/rebound/custom export REBOUND_SSH_PUBLIC_HOST=rebound export REBOUND_SSH_PUBLIC_PORT=2222 export REBOUND_SSH_SOCK_DIR=/var/lib/rebound/socks -export REBOUND_SSH_HOST_KEY=/etc/rebound/host.key \ No newline at end of file +export REBOUND_SSH_HOST_KEY=/etc/rebound/host.key +export REBOUND_HTTP_TEMPLATE_DATA_TITLE=Rebound +export REBOUND_HTTP_TEMPLATE_DATA_SSH_PUBLIC_HOST=127.0.0.1 +export REBOUND_HTTP_TEMPLATE_DATA_SSH_PUBLIC_PORT=8080 \ No newline at end of file diff --git a/misc/packaging/systemd/rebound.env b/misc/packaging/systemd/rebound.env index c50b24d..d1d362d 100644 --- a/misc/packaging/systemd/rebound.env +++ b/misc/packaging/systemd/rebound.env @@ -3,4 +3,7 @@ REBOUND_HTTP_CUSTOM_DIR=/var/lib/rebound/custom REBOUND_SSH_PUBLIC_HOST=rebound REBOUND_SSH_PUBLIC_PORT=8080 REBOUND_SSH_SOCK_DIR=/var/lib/rebound/socks -REBOUND_SSH_HOST_KEY=/var/lib/rebound/host.key \ No newline at end of file +REBOUND_SSH_HOST_KEY=/var/lib/rebound/host.key +REBOUND_HTTP_TEMPLATE_DATA_TITLE=Rebound +REBOUND_HTTP_TEMPLATE_DATA_SSH_PUBLIC_HOST=127.0.0.1 +REBOUND_HTTP_TEMPLATE_DATA_SSH_PUBLIC_PORT=8080 \ No newline at end of file diff --git a/server.go b/server.go index 841ca95..f522bc3 100644 --- a/server.go +++ b/server.go @@ -47,6 +47,7 @@ func (s *Server) Start() error { server := http.NewServer( http.WithCustomDir(s.opts.HTTP.CustomDir), + http.WithTemplateData(s.opts.HTTP.TemplateData), http.WithLogger(s.opts.HTTP.Logger), ) diff --git a/ssh/request_handler.go b/ssh/request_handler.go index 7b004aa..81d9dfb 100644 --- a/ssh/request_handler.go +++ b/ssh/request_handler.go @@ -80,6 +80,11 @@ func (s *Server) handleRequest(ctx ssh.Context, srv *ssh.Server, req *gossh.Requ addr := s.getSocketPath(sessionID) + if err := s.ensureFileDir(addr); err != nil { + s.log("[ERROR] %+v", errors.WithStack(err)) + return false, []byte("internal server error") + } + ln, err := net.Listen("unix", addr) if err != nil { s.log("[ERROR] %+v", errors.WithStack(err)) @@ -200,6 +205,15 @@ func (s *Server) getSocketPath(sessionID SessionID) string { return filepath.Join(s.opts.SockDir, fmt.Sprintf("%s.sock", sessionID)) } +func (s *Server) ensureFileDir(file string) error { + dir := filepath.Dir(file) + if err := os.MkdirAll(dir, os.FileMode(0750)); err != nil { + return errors.WithStack(err) + } + + return nil +} + func generateToken(length int) (string, error) { chars := []rune( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +