route/server/server.go

148 lines
3.1 KiB
Go
Raw Normal View History

package server
import (
2017-03-26 19:51:37 +00:00
"crypto/tls"
"errors"
"net"
"net/http"
2017-03-26 19:51:37 +00:00
"net/http/httputil"
"time"
"git.xeserv.us/xena/route/database"
2017-03-26 19:51:37 +00:00
"git.xeserv.us/xena/route/lib/tun2"
2017-04-29 03:08:11 +00:00
proto "git.xeserv.us/xena/route/proto"
2017-03-26 19:51:37 +00:00
"github.com/mtneug/pkg/ulid"
2017-04-29 06:22:00 +00:00
"github.com/oxtoacart/bpool"
2017-03-26 19:51:37 +00:00
"golang.org/x/crypto/acme/autocert"
2017-04-29 03:08:11 +00:00
"google.golang.org/grpc"
2017-04-29 05:30:32 +00:00
"google.golang.org/grpc/credentials"
)
// RPC constants
const (
RPCPort uint16 = 39453
)
// Server is the main server type
type Server struct {
2017-01-20 00:31:22 +00:00
cfg *Config
db database.Storage
ts *tun2.Server
2017-04-29 03:08:11 +00:00
*autocert.Manager
}
2017-01-20 00:31:22 +00:00
// Config configures Server
type Config struct {
BoltDBPath string `env:"BOLTDB_PATH,required"`
2017-03-27 04:39:19 +00:00
WebAddr string `env:"WEB_ADDR,required"`
SSLAddr string `env:"SSL_ADDR,required"`
BackendTCPAddr string `env:"BACKEND_TCP_ADDR,required"`
BackendKCPAddr string `env:"BACKEND_KCP_ADDR,required"`
GRPCAddr string `env:"GRPC_ADDR,required"`
2017-03-27 04:39:19 +00:00
DomainSuffix string `env:"DOMAIN_SUFFIX,required"`
ACMEEmail string `env:"ACME_EMAIL,required"`
2017-03-27 04:39:19 +00:00
CertKey *[32]byte
}
// New creates a new Server
2017-01-20 00:31:22 +00:00
func New(cfg Config) (*Server, error) {
if cfg.CertKey == nil {
return nil, errors.New("no cert decryption key, can't do anything")
}
db, err := database.NewBoltStorage(cfg.BoltDBPath, cfg.CertKey)
if err != nil {
return nil, err
}
2017-04-29 03:08:11 +00:00
m := &autocert.Manager{
2017-03-26 19:51:37 +00:00
Prompt: autocert.AcceptTOS,
Cache: database.Cache(db),
2017-03-26 19:51:37 +00:00
HostPolicy: nil,
2017-03-27 04:39:19 +00:00
Email: cfg.ACMEEmail,
2017-03-26 19:51:37 +00:00
}
tcfg := &tun2.ServerConfig{
TCPAddr: cfg.BackendTCPAddr,
KCPAddr: cfg.BackendKCPAddr,
2017-03-26 19:51:37 +00:00
TLSConfig: &tls.Config{
GetCertificate: m.GetCertificate,
},
Storage: &storageWrapper{
Storage: db,
},
2017-03-26 19:51:37 +00:00
}
2017-03-26 19:51:37 +00:00
ts, err := tun2.NewServer(tcfg)
if err != nil {
return nil, err
}
s := &Server{
cfg: &cfg,
db: db,
ts: ts,
2017-03-26 19:51:37 +00:00
Manager: m,
2017-01-28 00:14:05 +00:00
}
s.ts = ts
go ts.ListenAndServe()
2017-04-29 05:30:32 +00:00
gs := grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
2017-04-29 05:32:01 +00:00
GetCertificate: m.GetCertificate,
InsecureSkipVerify: true,
2017-04-29 05:30:32 +00:00
})))
2017-04-29 03:08:11 +00:00
proto.RegisterRoutesServer(gs, &Route{Server: s})
proto.RegisterTokensServer(gs, &Token{Server: s})
2017-04-29 05:30:32 +00:00
l, err := net.Listen("tcp", cfg.GRPCAddr)
2017-04-29 03:08:11 +00:00
if err != nil {
return nil, err
}
go gs.Serve(l)
return s, nil
}
// Director removes headers that are typically stripped off at request ingress.
func (s *Server) Director(r *http.Request) {
2017-01-22 17:49:14 +00:00
r.Header.Del("X-Forwarded-For")
2017-03-26 20:47:42 +00:00
r.Header.Del("X-Client-Ip")
}
2017-01-22 17:49:14 +00:00
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2017-01-22 17:49:14 +00:00
if r.Header.Get("X-Tor2web") != "" {
http.Error(w, "tor2web proxy use is not allowed", 400)
return
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
2017-03-26 20:47:42 +00:00
r.Header.Set("X-Remote-Ip", host)
r.Header.Set("X-Request-Ingress", time.Now().Format(time.RFC3339))
2017-01-20 01:17:18 +00:00
2017-03-26 19:51:37 +00:00
rid := ulid.New().String()
2017-01-23 16:03:14 +00:00
r.Header.Set("X-Request-Id", rid)
w.Header().Set("X-Request-Id", rid)
2017-02-08 02:25:14 +00:00
// http://www.gnuterrypratchett.com/
2017-03-26 19:51:37 +00:00
w.Header().Set("X-Clacks-Overhead", "GNU Ashlynn")
2017-02-08 02:25:14 +00:00
2017-03-26 19:51:37 +00:00
rp := &httputil.ReverseProxy{
2017-03-26 20:02:22 +00:00
Director: s.Director,
2017-03-26 19:51:37 +00:00
Transport: s.ts,
FlushInterval: 1 * time.Second,
2017-04-29 06:22:00 +00:00
BufferPool: bpool.NewBytePool(256, 4096),
2017-03-26 19:51:37 +00:00
}
rp.ServeHTTP(w, r)
}