server: rip out tunnel

This commit is contained in:
Cadey Ratio 2017-03-26 12:51:37 -07:00
parent 87be7e8a30
commit 5fd805b5dd
1 changed files with 39 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package server
import ( import (
"crypto/rsa" "crypto/rsa"
"crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors" "errors"
@ -9,6 +10,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/http/httputil"
"net/rpc" "net/rpc"
"os" "os"
"path/filepath" "path/filepath"
@ -17,12 +19,14 @@ import (
"git.xeserv.us/xena/route/database" "git.xeserv.us/xena/route/database"
"git.xeserv.us/xena/route/lib/elfs" "git.xeserv.us/xena/route/lib/elfs"
"git.xeserv.us/xena/route/lib/tunnel" "git.xeserv.us/xena/route/lib/tun2"
"git.xeserv.us/xena/route/routerpc" "git.xeserv.us/xena/route/routerpc"
"git.xeserv.us/xena/route/utils" "git.xeserv.us/xena/route/utils"
"github.com/Xe/uuid" "github.com/Xe/uuid"
"github.com/Yawning/bulb" "github.com/Yawning/bulb"
"github.com/brandur/simplebox" "github.com/brandur/simplebox"
"github.com/mtneug/pkg/ulid"
"golang.org/x/crypto/acme/autocert"
) )
// RPC constants // RPC constants
@ -40,7 +44,7 @@ type Server struct {
rpcS *rpc.Server rpcS *rpc.Server
rpcAddr string rpcAddr string
ts *tunnel.Server ts *tun2.Server
CertCache *database.CertCache CertCache *database.CertCache
} }
@ -51,6 +55,7 @@ type Config struct {
RethinkDBHost, RethinkDBDatabase string RethinkDBHost, RethinkDBDatabase string
TorDataDir, TorHashedPassword, TorPassword string TorDataDir, TorHashedPassword, TorPassword string
WebPort, DomainSuffix, SSLPort, GRPCClientPort string WebPort, DomainSuffix, SSLPort, GRPCClientPort string
BackendPort, KCPPort string
CertKey *[32]byte CertKey *[32]byte
} }
@ -99,11 +104,6 @@ func New(cfg Config) (*Server, error) {
rpcs := rpc.NewServer() rpcs := rpc.NewServer()
ts, err := tunnel.NewServer(&tunnel.ServerConfig{})
if err != nil {
return nil, err
}
s := &Server{ s := &Server{
cfg: &cfg, cfg: &cfg,
@ -113,33 +113,46 @@ func New(cfg Config) (*Server, error) {
rpcS: rpcs, rpcS: rpcs,
rpcAddr: l.Addr().String(), rpcAddr: l.Addr().String(),
ts: ts,
CertCache: &database.CertCache{ CertCache: &database.CertCache{
DB: db, DB: db,
}, },
} }
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: s.CertCache,
HostPolicy: nil,
Email: "xena@yolo-swag.com",
}
if cfg.CertKey != nil { if cfg.CertKey != nil {
s.CertCache.SimpleBox = simplebox.NewFromSecretKey(cfg.CertKey) s.CertCache.SimpleBox = simplebox.NewFromSecretKey(cfg.CertKey)
} }
tcfg := &tun2.ServerConfig{
TCPAddr: cfg.BackendPort,
KCPAddr: cfg.KCPPort,
TLSConfig: &tls.Config{
GetCertificate: m.GetCertificate,
},
Storage: s.db,
}
ts, err := tun2.NewServer(tcfg)
if err != nil {
return nil, err
}
s.ts = ts
rpcs.RegisterName("Urls", &RPCServer{Server: s}) rpcs.RegisterName("Urls", &RPCServer{Server: s})
go rpcs.Accept(l) go rpcs.Accept(l)
log.Println("rpc at tcp://" + l.Addr().String())
err = s.restore() err = s.restore()
if err != nil { if err != nil {
return nil, err return nil, err
} }
grpcl, err := net.Listen("tcp", ":"+cfg.GRPCClientPort)
if err != nil {
return nil, err
}
// XXX HACK
s.ts.AddAddr(grpcl, nil, "f3724661-af05-41bc-ad99-753b9d631f43")
return s, nil return s, nil
} }
@ -164,12 +177,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Header.Set("X-Remote-IP", host) r.Header.Set("X-Remote-IP", host)
r.Header.Set("X-Request-Ingress", time.Now().String()) r.Header.Set("X-Request-Ingress", time.Now().String())
rid := uuid.New() rid := ulid.New().String()
r.Header.Set("X-Request-Id", rid) r.Header.Set("X-Request-Id", rid)
w.Header().Set("X-Request-Id", rid) w.Header().Set("X-Request-Id", rid)
// http://www.gnuterrypratchett.com/ // http://www.gnuterrypratchett.com/
w.Header().Set("X-Clacks-Overhead", "GNU Terry Pratchett") w.Header().Set("X-Clacks-Overhead", "GNU Ashlynn")
if strings.HasSuffix(r.Host, ".onion") { if strings.HasSuffix(r.Host, ".onion") {
w.Header().Add("DNT", "1") w.Header().Add("DNT", "1")
@ -179,7 +192,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.rpcS.ServeHTTP(w, r) s.rpcS.ServeHTTP(w, r)
return return
} }
s.ts.ServeHTTP(w, r)
rp := &httputil.ReverseProxy{
Transport: s.ts,
FlushInterval: 1 * time.Second,
}
rp.ServeHTTP(w, r)
} }
func (s *Server) restore() error { func (s *Server) restore() error {
@ -202,9 +221,6 @@ func (s *Server) restore() error {
return err return err
} }
s.ts.AddHost(rt.Hostname, rt.Token)
s.ts.AddHost(rt.OnionHostname, rt.Token)
log.Printf("added: %s (%s)", rt.Hostname, rt.OnionHostname) log.Printf("added: %s (%s)", rt.Hostname, rt.OnionHostname)
} }
@ -248,13 +264,10 @@ func (rs *RPCServer) AddHost(req routerpc.AddHostRequest, resp *routerpc.AddHost
resp.PrivKey = pKey resp.PrivKey = pKey
if req.Hostname != "" { if req.Hostname != "" {
rs.Server.ts.AddHost(req.Hostname, token)
resp.Hostname = req.Hostname resp.Hostname = req.Hostname
} else { } else {
resp.Hostname = elfs.MakeName() + rs.cfg.DomainSuffix resp.Hostname = elfs.MakeName() + rs.cfg.DomainSuffix
rs.ts.AddHost(resp.Hostname, token)
} }
rs.Server.ts.AddHost(resp.OnionHostname, token)
err = rs.db.SaveRoute(resp) err = rs.db.SaveRoute(resp)
if err != nil { if err != nil {