feat: add http server with a page serving service informations
This commit is contained in:
parent
56d7174b96
commit
fdaffca43f
|
@ -3,4 +3,5 @@
|
|||
/bin
|
||||
/.env
|
||||
/socks
|
||||
/host.key
|
||||
/host.key
|
||||
/custom
|
|
@ -7,37 +7,48 @@ import (
|
|||
"os/signal"
|
||||
|
||||
"forge.cadoles.com/wpetit/rebound"
|
||||
"github.com/caarlos0/env/v6"
|
||||
"forge.cadoles.com/wpetit/rebound/http"
|
||||
"forge.cadoles.com/wpetit/rebound/ssh"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
opts := rebound.DefaultOptions()
|
||||
if err := env.Parse(opts); err != nil {
|
||||
|
||||
if err := opts.ParseEnv(); err != nil {
|
||||
log.Fatalf("[ERROR] %+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
// Global Options
|
||||
address := flag.String("address", opts.Address, "server listening address")
|
||||
sockDir := flag.String("sock-dir", opts.SockDir, "sock directory")
|
||||
publicPort := flag.Uint("public-port", opts.PublicPort, "public port")
|
||||
publicHost := flag.String("public-host", opts.PublicHost, "public host")
|
||||
hostKey := flag.String("host-key", opts.HostKey, "host key")
|
||||
|
||||
// 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.WithSockDir(*sockDir),
|
||||
rebound.WithPublicPort(*publicPort),
|
||||
rebound.WithPublicHost(*publicHost),
|
||||
rebound.WithHostKey(*hostKey),
|
||||
rebound.WithSSHOption(
|
||||
ssh.WithSockDir(*sockDir),
|
||||
ssh.WithPublicHost(*publicHost),
|
||||
ssh.WithPublicPort(*publicPort),
|
||||
ssh.WithHostKey(*hostKey),
|
||||
),
|
||||
rebound.WitHTTPOption(
|
||||
http.WithCustomDir(*customDir),
|
||||
),
|
||||
)
|
||||
|
||||
go func() {
|
||||
if err := server.Run(); err != nil {
|
||||
log.Fatalf("[FATAL] %+v", errors.WithStack(err))
|
||||
}
|
||||
}()
|
||||
if err := server.Start(); err != nil {
|
||||
log.Fatalf("[FATAL] %+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
|
|
3
go.mod
3
go.mod
|
@ -3,7 +3,8 @@ module forge.cadoles.com/wpetit/rebound
|
|||
go 1.21.0
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.10.1
|
||||
github.com/caarlos0/env/v9 v9.0.0
|
||||
github.com/dschmidt/go-layerfs v0.1.0
|
||||
github.com/gliderlabs/ssh v0.3.5
|
||||
github.com/pkg/errors v0.9.1
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d
|
||||
|
|
16
go.sum
16
go.sum
|
@ -1,11 +1,21 @@
|
|||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
||||
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
|
||||
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
|
||||
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
|
||||
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dschmidt/go-layerfs v0.1.0 h1:jE6aHDfjNzS/31DS48th6EkmELwTa1Uf+aO4jRkBs3U=
|
||||
github.com/dschmidt/go-layerfs v0.1.0/go.mod h1:m62aff0hn23Q/tQBRiNSeLD7EUuimDvsuCvCpzBr3Gw=
|
||||
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
|
||||
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef h1:NKxTG6GVGbfMXc2mIk+KphcH6hagbVXhcFkbTgYleTI=
|
||||
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef/go.mod h1:tcaRap0jS3eifrEEllL6ZMd9dg8IlDpi2S1oARrQ+NI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
|
@ -23,3 +33,5 @@ golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuX
|
|||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,29 @@
|
|||
package http
|
||||
|
||||
import "log"
|
||||
|
||||
type Options struct {
|
||||
Logger func(message string, args ...any)
|
||||
CustomDir string `env:"CUSTOM_DIR"`
|
||||
}
|
||||
|
||||
type OptionFunc func(*Options)
|
||||
|
||||
func DefaultOptions() *Options {
|
||||
return &Options{
|
||||
Logger: log.Printf,
|
||||
CustomDir: "",
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger func(message string, args ...any)) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func WithCustomDir(customDir string) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.CustomDir = customDir
|
||||
}
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/dschmidt/go-layerfs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
var embeddedTemplates embed.FS
|
||||
|
||||
//go:embed assets
|
||||
var embeddedAssets embed.FS
|
||||
|
||||
type Server struct {
|
||||
http *http.Server
|
||||
opts *Options
|
||||
templates template.Template
|
||||
}
|
||||
|
||||
func (s *Server) serveHomepage(w http.ResponseWriter, r *http.Request) {
|
||||
data := struct {
|
||||
Title string
|
||||
}{
|
||||
Title: "Rebound",
|
||||
}
|
||||
|
||||
s.renderTemplate(w, "index", data)
|
||||
}
|
||||
|
||||
func (s *Server) Serve(l net.Listener) error {
|
||||
templatesFilesystem, err := s.getCustomizedFilesystem(embeddedTemplates)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if err := s.parseTemplates(templatesFilesystem); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
assetsFilesystem, err := s.getCustomizedFilesystem(embeddedAssets)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/assets/", http.FileServer(http.FS(assetsFilesystem)))
|
||||
mux.HandleFunc("/", s.serveHomepage)
|
||||
|
||||
httpServer := &http.Server{
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
s.http = httpServer
|
||||
|
||||
if err := s.http.Serve(l); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) parseTemplates(fs fs.FS) error {
|
||||
templates, err := template.ParseFS(fs, "templates/*.html")
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
s.templates = *templates
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) renderTemplate(w http.ResponseWriter, name string, data any) {
|
||||
var buff bytes.Buffer
|
||||
|
||||
if err := s.templates.ExecuteTemplate(&buff, name, data); err != nil {
|
||||
s.log("[ERROR] %+s", errors.WithStack(err))
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := io.Copy(w, &buff); err != nil {
|
||||
s.log("[ERROR] %+s", errors.WithStack(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) log(message string, args ...any) {
|
||||
s.opts.Logger(message, args...)
|
||||
}
|
||||
|
||||
func (s *Server) getCustomizedFilesystem(base fs.FS) (fs.FS, error) {
|
||||
filesystems := []fs.FS{}
|
||||
|
||||
if s.opts.CustomDir != "" {
|
||||
absPath, err := filepath.Abs(s.opts.CustomDir)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
filesystems = append(filesystems, os.DirFS(absPath))
|
||||
}
|
||||
|
||||
filesystems = append(filesystems, base)
|
||||
|
||||
return layerfs.New(filesystems...), nil
|
||||
}
|
||||
|
||||
func NewServer(funcs ...OptionFunc) *Server {
|
||||
opts := DefaultOptions()
|
||||
for _, fn := range funcs {
|
||||
fn(opts)
|
||||
}
|
||||
|
||||
return &Server{
|
||||
opts: opts,
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{{ define "advertising" }}{{ end }}
|
|
@ -0,0 +1,9 @@
|
|||
{{ define "footer" }}
|
||||
<footer class="footer">
|
||||
<div class="container">
|
||||
<div class="content has-text-centered">
|
||||
Ce service est propulsé par Rebound, un logiciel libre diffusé sous licence <a href="#">AGPL-3.0</a>.
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
{{ end }}
|
|
@ -0,0 +1,6 @@
|
|||
{{ define "head" }}
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{ .Title }}</title>
|
||||
<link rel="stylesheet" href="assets/bulma-0.9.4.min.css">
|
||||
{{ end }}
|
|
@ -0,0 +1,31 @@
|
|||
{{ define "index" }}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
{{ template "head" }}
|
||||
</head>
|
||||
<body>
|
||||
<section class="section">
|
||||
<div class="container">
|
||||
<h1 class="title is-size-1">
|
||||
Besoin de vous échapper ?
|
||||
</h1>
|
||||
<p class="subtitle is-size-3">
|
||||
Bienvenue sur <strong>Rebound</strong>!
|
||||
</p>
|
||||
<div class="content">
|
||||
<p>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 <abbr title="Network Address Traversal">NAT</abbr>.</p>
|
||||
<p>Pour l'utiliser <strong>un simple client SSH suffit !</strong></p>
|
||||
<pre class="has-background-dark has-text-white-ter is-family-monospace">ssh -R 0:127.0.0.1:<span class="has-text-info"><port></span> rebound@rebound.cadol.es</pre>
|
||||
<p class="is-italic">Où <span class="has-text-info"><port></span> est à remplacer par le port du service
|
||||
s'exécutant sur votre machine en local.</span>
|
||||
<p>Une fois connecté, suivez les instructions. 😉</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{{ template "advertising" . }}
|
||||
{{ template "footer" . }}
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
|
@ -1,5 +1,6 @@
|
|||
**/*.go
|
||||
diagram.txt
|
||||
http/templates/**
|
||||
http/assets/**
|
||||
.env
|
||||
Makefile {
|
||||
prep: make build
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package rebound
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (s *Server) muxListener(l net.Listener) (ssh net.Listener, other net.Listener) {
|
||||
sshListener, otherListener := newListener(l), newListener(l)
|
||||
go func() {
|
||||
for {
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
return
|
||||
}
|
||||
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if err := conn.SetReadDeadline(time.Now().Add(time.Second * 10)); err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
bconn := bufferedConn{conn, bufio.NewReaderSize(conn, 3)}
|
||||
|
||||
p, err := bconn.Peek(3)
|
||||
if err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
selectedListener := otherListener
|
||||
if prefix := string(p); prefix == "SSH" {
|
||||
s.log("[INFO] new ssh connection from '%s'", conn.RemoteAddr())
|
||||
selectedListener = sshListener
|
||||
} else {
|
||||
s.log("[INFO] new http connection from '%s'", conn.RemoteAddr())
|
||||
}
|
||||
|
||||
if selectedListener.accept != nil {
|
||||
selectedListener.accept <- bconn
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return sshListener, otherListener
|
||||
}
|
||||
|
||||
type listener struct {
|
||||
accept chan net.Conn
|
||||
net.Listener
|
||||
}
|
||||
|
||||
func newListener(l net.Listener) *listener {
|
||||
return &listener{
|
||||
make(chan net.Conn),
|
||||
l,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *listener) Accept() (net.Conn, error) {
|
||||
if l.accept == nil {
|
||||
return nil, errors.New("listener closed")
|
||||
}
|
||||
return <-l.accept, nil
|
||||
}
|
||||
|
||||
func (l *listener) Close() error {
|
||||
close(l.accept)
|
||||
l.accept = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
type bufferedConn struct {
|
||||
net.Conn
|
||||
r *bufio.Reader
|
||||
}
|
||||
|
||||
func (b bufferedConn) Peek(n int) ([]byte, error) {
|
||||
return b.r.Peek(n)
|
||||
}
|
||||
|
||||
func (b bufferedConn) Read(p []byte) (int, error) {
|
||||
return b.r.Read(p)
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package rebound
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"forge.cadoles.com/wpetit/rebound/http"
|
||||
"forge.cadoles.com/wpetit/rebound/ssh"
|
||||
"github.com/caarlos0/env/v9"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Address string `env:"REBOUND_ADDRESS"`
|
||||
Logger func(message string, args ...any)
|
||||
SSH *ssh.Options `envPrefix:"REBOUND_SSH_"`
|
||||
HTTP *http.Options `envPrefix:"REBOUND_HTTP_"`
|
||||
}
|
||||
|
||||
func (o *Options) ParseEnv() error {
|
||||
if err := env.Parse(o); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type OptionFunc func(*Options)
|
||||
|
||||
func DefaultOptions() *Options {
|
||||
return &Options{
|
||||
Address: "127.0.0.1:2222",
|
||||
Logger: log.Printf,
|
||||
SSH: ssh.DefaultOptions(),
|
||||
HTTP: http.DefaultOptions(),
|
||||
}
|
||||
}
|
||||
|
||||
func WithAddress(addr string) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.Address = addr
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger func(message string, args ...any)) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
opts.SSH.Logger = logger
|
||||
opts.HTTP.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func WithSSHOption(funcs ...ssh.OptionFunc) func(*Options) {
|
||||
return func(o *Options) {
|
||||
for _, fn := range funcs {
|
||||
fn(o.SSH)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WitHTTPOption(funcs ...http.OptionFunc) func(*Options) {
|
||||
return func(o *Options) {
|
||||
for _, fn := range funcs {
|
||||
fn(o.HTTP)
|
||||
}
|
||||
}
|
||||
}
|
144
server.go
144
server.go
|
@ -1,129 +1,78 @@
|
|||
package rebound
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gliderlabs/ssh"
|
||||
"forge.cadoles.com/wpetit/rebound/http"
|
||||
"forge.cadoles.com/wpetit/rebound/ssh"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
gossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
ssh *ssh.Server
|
||||
opts *Options
|
||||
|
||||
sessionManager *SessionManager
|
||||
forwards map[string]net.Listener
|
||||
requestHandlerLock sync.Mutex
|
||||
listener net.Listener
|
||||
opts *Options
|
||||
}
|
||||
|
||||
func (s *Server) Run() error {
|
||||
if err := s.initSSHServer(); err != nil {
|
||||
func (s *Server) Start() error {
|
||||
s.log("[INFO] listening on %s", s.opts.Address)
|
||||
|
||||
listener, err := net.Listen("tcp", s.opts.Address)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
s.log("listening on %s", s.opts.Address)
|
||||
s.listener = listener
|
||||
|
||||
if err := s.ssh.ListenAndServe(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
sshListener, httpListener := s.muxListener(listener)
|
||||
|
||||
go func() {
|
||||
defer listener.Close()
|
||||
|
||||
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),
|
||||
)
|
||||
|
||||
if err := server.Serve(sshListener); err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
listener.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer listener.Close()
|
||||
|
||||
server := http.NewServer(
|
||||
http.WithCustomDir(s.opts.HTTP.CustomDir),
|
||||
http.WithLogger(s.opts.HTTP.Logger),
|
||||
)
|
||||
|
||||
if err := server.Serve(httpListener); err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) Stop() error {
|
||||
if s.ssh == nil {
|
||||
if s.listener == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
s.log("stopping on '%s'", s.opts.Address)
|
||||
|
||||
if err := s.ssh.Close(); err != nil {
|
||||
if err := s.listener.Close(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) initSSHServer() error {
|
||||
signer, err := s.loadOrCreateSigner()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
server := &ssh.Server{
|
||||
Addr: s.opts.Address,
|
||||
HostSigners: []ssh.Signer{signer},
|
||||
Handler: ssh.Handler(s.handleSession),
|
||||
RequestHandlers: map[string]ssh.RequestHandler{
|
||||
"tcpip-forward": s.handleRequest,
|
||||
"cancel-tcpip-forward": s.handleRequest,
|
||||
},
|
||||
ChannelHandlers: map[string]ssh.ChannelHandler{
|
||||
"direct-tcpip": s.handleDirectTCP,
|
||||
"session": ssh.DefaultSessionHandler,
|
||||
},
|
||||
}
|
||||
|
||||
s.ssh = server
|
||||
s.listener = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) loadOrCreateSigner() (ssh.Signer, error) {
|
||||
var (
|
||||
signer gossh.Signer
|
||||
)
|
||||
|
||||
s.log("reading host key from '%s'", s.opts.HostKey)
|
||||
|
||||
data, err := os.ReadFile(s.opts.HostKey)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
s.log("host key cannot be found, generating one")
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
signer, err = gossh.NewSignerFromKey(key)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
pem := pem.EncodeToMemory(
|
||||
&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(key),
|
||||
},
|
||||
)
|
||||
|
||||
s.log("saving host key to '%s'", s.opts.HostKey)
|
||||
if err := os.WriteFile(s.opts.HostKey, pem, fs.FileMode(0640)); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
} else {
|
||||
signer, err = gossh.ParsePrivateKey(data)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
return signer, nil
|
||||
}
|
||||
|
||||
func (s *Server) log(message string, args ...any) {
|
||||
s.opts.Logger(message, args...)
|
||||
}
|
||||
|
@ -135,7 +84,6 @@ func NewServer(funcs ...OptionFunc) *Server {
|
|||
}
|
||||
|
||||
return &Server{
|
||||
opts: opts,
|
||||
sessionManager: NewSessionManager(30 * time.Second),
|
||||
opts: opts,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package rebound
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,21 +1,19 @@
|
|||
package rebound
|
||||
package ssh
|
||||
|
||||
import "log"
|
||||
|
||||
type Options struct {
|
||||
Address string `env:"REBOUND_ADDRESS"`
|
||||
Logger func(message string, args ...any)
|
||||
SockDir string `env:"REBOUND_SOCK_DIR"`
|
||||
PublicPort uint `env:"REBOUND_PUBLIC_PORT"`
|
||||
PublicHost string `env:"REBOUND_PUBLIC_HOST"`
|
||||
HostKey string `env:"REBOUND_HOST_KEY"`
|
||||
SockDir string `env:"SOCK_DIR"`
|
||||
PublicPort uint `env:"PUBLIC_PORT"`
|
||||
PublicHost string `env:"PUBLIC_HOST"`
|
||||
HostKey string `env:"HOST_KEY"`
|
||||
}
|
||||
|
||||
type OptionFunc func(*Options)
|
||||
|
||||
func DefaultOptions() *Options {
|
||||
return &Options{
|
||||
Address: "127.0.0.1:2222",
|
||||
SockDir: "./socks",
|
||||
Logger: log.Printf,
|
||||
PublicPort: 2222,
|
||||
|
@ -24,12 +22,6 @@ func DefaultOptions() *Options {
|
|||
}
|
||||
}
|
||||
|
||||
func WithAddress(addr string) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.Address = addr
|
||||
}
|
||||
}
|
||||
|
||||
func WithSockDir(addr string) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.SockDir = addr
|
||||
|
@ -53,3 +45,9 @@ func WithHostKey(key string) func(*Options) {
|
|||
opts.HostKey = key
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogger(logger func(message string, args ...any)) func(*Options) {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package rebound
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
|
@ -0,0 +1,124 @@
|
|||
package ssh
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gliderlabs/ssh"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
gossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
ssh *ssh.Server
|
||||
opts *Options
|
||||
|
||||
sessionManager *SessionManager
|
||||
forwards map[string]net.Listener
|
||||
requestHandlerLock sync.Mutex
|
||||
}
|
||||
|
||||
func (s *Server) Serve(l net.Listener) error {
|
||||
if err := s.initSSHServer(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if err := s.ssh.Serve(l); err != nil {
|
||||
s.log("[ERROR] %+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) initSSHServer() error {
|
||||
signer, err := s.loadOrCreateSigner()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
server := &ssh.Server{
|
||||
HostSigners: []ssh.Signer{signer},
|
||||
Handler: ssh.Handler(s.handleSession),
|
||||
RequestHandlers: map[string]ssh.RequestHandler{
|
||||
"tcpip-forward": s.handleRequest,
|
||||
"cancel-tcpip-forward": s.handleRequest,
|
||||
},
|
||||
ChannelHandlers: map[string]ssh.ChannelHandler{
|
||||
"direct-tcpip": s.handleDirectTCP,
|
||||
"session": ssh.DefaultSessionHandler,
|
||||
},
|
||||
}
|
||||
|
||||
s.ssh = server
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) loadOrCreateSigner() (ssh.Signer, error) {
|
||||
var (
|
||||
signer gossh.Signer
|
||||
)
|
||||
|
||||
s.log("reading host key from '%s'", s.opts.HostKey)
|
||||
|
||||
data, err := os.ReadFile(s.opts.HostKey)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
s.log("host key cannot be found, generating one")
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
signer, err = gossh.NewSignerFromKey(key)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
pem := pem.EncodeToMemory(
|
||||
&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(key),
|
||||
},
|
||||
)
|
||||
|
||||
s.log("saving host key to '%s'", s.opts.HostKey)
|
||||
if err := os.WriteFile(s.opts.HostKey, pem, fs.FileMode(0640)); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
} else {
|
||||
signer, err = gossh.ParsePrivateKey(data)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
return signer, nil
|
||||
}
|
||||
|
||||
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{
|
||||
opts: opts,
|
||||
sessionManager: NewSessionManager(30 * time.Second),
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package rebound
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"bytes"
|
|
@ -1,4 +1,4 @@
|
|||
package rebound
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"errors"
|
Loading…
Reference in New Issue