make everything work again

This commit is contained in:
Cadey Ratio 2017-01-19 16:31:22 -08:00
parent f58c7d6d86
commit cb6e57b670
5 changed files with 158 additions and 77 deletions

20
cmd/helloserver/main.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"flag"
"fmt"
"net/http"
)
var (
port = flag.String("port", "9090", "HTTP port to listen on")
)
func main() {
flag.Parse()
http.ListenAndServe(":"+*port, http.HandlerFunc(handle))
}
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello garukun, it's me xena. Hire me and I can make something like this for vungle.")
}

View File

@ -31,7 +31,7 @@ func main() {
flagenv.Parse() flagenv.Parse()
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
s, err := server.New(&server.ServerConfig{ s, err := server.New(server.Config{
ControlHost: *controlHost, ControlHost: *controlHost,
ControlKeyFile: *controlKeyFile, ControlKeyFile: *controlKeyFile,
RethinkDBHost: *rethinkDBHost, RethinkDBHost: *rethinkDBHost,

View File

@ -1,28 +1,26 @@
package server package server
import ( import (
"crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net" "net"
"net/http" "net/http"
"net/rpc" "net/rpc"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"time" "time"
"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/routerpc" "git.xeserv.us/xena/route/routerpc"
"git.xeserv.us/xena/route/utils"
"github.com/Xe/uuid" "github.com/Xe/uuid"
"github.com/Yawning/bulb" "github.com/Yawning/bulb"
"github.com/koding/tunnel" "github.com/koding/tunnel"
"github.com/sycamoreone/orc/tor"
) )
// RPC constants // RPC constants
@ -32,13 +30,10 @@ const (
// Server is the main server type // Server is the main server type
type Server struct { type Server struct {
cfg *ServerConfig cfg *Config
db *database.DB db *database.DB
tor *Tor
torCon *bulb.Conn
torCmd *tor.Cmd
torControlPath string
rpcS *rpc.Server rpcS *rpc.Server
rpcAddr string rpcAddr string
@ -46,8 +41,8 @@ type Server struct {
ts *tunnel.Server ts *tunnel.Server
} }
// ServerConfig configures Server // Config configures Server
type ServerConfig struct { type Config struct {
ControlHost, ControlKeyFile string ControlHost, ControlKeyFile string
RethinkDBHost, RethinkDBDatabase string RethinkDBHost, RethinkDBDatabase string
TorDataDir, TorHashedPassword, TorPassword string TorDataDir, TorHashedPassword, TorPassword string
@ -55,7 +50,7 @@ type ServerConfig struct {
} }
// New creates a new Server // New creates a new Server
func New(cfg *ServerConfig) (*Server, error) { func New(cfg Config) (*Server, error) {
db, err := database.New(cfg.RethinkDBHost, cfg.RethinkDBDatabase) db, err := database.New(cfg.RethinkDBHost, cfg.RethinkDBDatabase)
if err != nil { if err != nil {
return nil, err return nil, err
@ -66,36 +61,12 @@ func New(cfg *ServerConfig) (*Server, error) {
return nil, err return nil, err
} }
torControlPath := filepath.Join(cfg.TorDataDir, fmt.Sprintf("%d.sock", rand.Int63())) t, err := StartTor(TorConfig{
DataDir: cfg.TorDataDir,
tc := tor.NewConfig() HashedControlPassword: cfg.TorHashedPassword,
tc.Set("DataDirectory", cfg.TorDataDir) ClearPassword: cfg.TorPassword,
tc.Set("HashedControlPassword", cfg.TorHashedPassword) Timeout: 30 * time.Second,
tc.Set("SocksPort", "0") })
cp := rand.Intn(64512) // 64k - 1k
tc.Set("ControlPort", cp)
tc.Timeout = 30 * time.Second
log.Println(tc.ToCmdLineFormat())
tcmd, err := tor.NewCmd(tc)
if err != nil {
return nil, err
}
err = tcmd.Start()
if err != nil {
return nil, err
}
time.Sleep(5 * time.Second)
bc, err := bulb.Dial("tcp", "127.0.0.1:"+strconv.Itoa(cp))
if err != nil {
return nil, err
}
err = bc.Authenticate(cfg.TorPassword)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -111,20 +82,12 @@ func New(cfg *ServerConfig) (*Server, error) {
return nil, err return nil, err
} }
var block *pem.Block pKey, err := utils.PemToRSAPrivateKey(data)
block, _ = pem.Decode([]byte(data))
pKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = bc.AddOnion([]bulb.OnionPortSpec{ _, err = t.AddOnion(pKey, RPCPort, l.Addr().String())
bulb.OnionPortSpec{
VirtPort: RPCPort,
Target: l.Addr().String(),
},
}, pKey, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -137,13 +100,10 @@ func New(cfg *ServerConfig) (*Server, error) {
} }
s := &Server{ s := &Server{
cfg: cfg, cfg: &cfg,
db: db, db: db,
tor: t,
torCon: bc,
torCmd: tcmd,
torControlPath: torControlPath,
rpcS: rpcs, rpcS: rpcs,
rpcAddr: l.Addr().String(), rpcAddr: l.Addr().String(),
@ -189,11 +149,7 @@ func (s *Server) restore() error {
return err return err
} }
ports := []bulb.OnionPortSpec{ _, err = s.tor.AddOnion(pKey, 80, "127.0.0.1:"+s.cfg.WebPort)
genPortSpec(80, "127.0.0.1:"+s.cfg.WebPort),
}
_, err = s.torCon.AddOnion(ports, pKey, false)
if err != nil { if err != nil {
return err return err
} }
@ -219,6 +175,7 @@ type RPCServer struct {
*Server *Server
} }
// AddHost adds a host to the server.
func (rs *RPCServer) AddHost(req routerpc.AddHostRequest, resp *routerpc.AddHostResponse) error { func (rs *RPCServer) AddHost(req routerpc.AddHostRequest, resp *routerpc.AddHostResponse) error {
if req.APIKey != "hunter2" { if req.APIKey != "hunter2" {
return errors.New("invalid api key") return errors.New("invalid api key")
@ -226,12 +183,13 @@ func (rs *RPCServer) AddHost(req routerpc.AddHostRequest, resp *routerpc.AddHost
token := uuid.New() token := uuid.New()
oi, err := rs.torCon.AddOnion([]bulb.OnionPortSpec{ var pKey *rsa.PrivateKey
bulb.OnionPortSpec{
VirtPort: 80, if kk, ok := req.PrivKey.(*rsa.PrivateKey); ok {
Target: "127.0.0.1:" + rs.cfg.WebPort, pKey = kk
}, }
}, req.PrivKey, false)
oi, err := rs.tor.AddOnion(pKey, 80, "127.0.0.1:"+rs.cfg.WebPort)
if err != nil { if err != nil {
return err return err
} }
@ -248,12 +206,6 @@ func (rs *RPCServer) AddHost(req routerpc.AddHostRequest, resp *routerpc.AddHost
} }
rs.Server.ts.AddHost(resp.OnionHostname, token) rs.Server.ts.AddHost(resp.OnionHostname, token)
if oi.PrivateKey != nil {
resp.PrivKey = oi.PrivateKey
} else {
resp.PrivKey = req.PrivKey
}
err = rs.db.SaveRoute(resp) err = rs.db.SaveRoute(resp)
if err != nil { if err != nil {
return err return err

80
server/tor.go Normal file
View File

@ -0,0 +1,80 @@
package server
import (
"crypto/rsa"
"log"
"math/rand"
"strconv"
"time"
"github.com/Yawning/bulb"
"github.com/sycamoreone/orc/tor"
)
// TorConfig is a wrapper struct for tor configuration.
type TorConfig struct {
DataDir string
HashedControlPassword string
ClearPassword string
Timeout time.Duration
}
// StartTor starts a new instance of tor or doesn't with the reason why.
func StartTor(cfg TorConfig) (*Tor, error) {
tc := tor.NewConfig()
tc.Set("DataDirectory", cfg.DataDir)
tc.Set("HashedControlPassword", cfg.HashedControlPassword)
tc.Set("SocksPort", "0")
cp := rand.Intn(64512)
tc.Set("ControlPort", cp)
tc.Timeout = cfg.Timeout
tcmd, err := tor.NewCmd(tc)
if err != nil {
return nil, err
}
err = tcmd.Start()
if err != nil {
return nil, err
}
log.Println("tor started, sleeping for a few seconds for it to settle...")
time.Sleep(5 * time.Second)
bc, err := bulb.Dial("tcp", "127.0.0.1:"+strconv.Itoa(cp))
if err != nil {
return nil, err
}
err = bc.Authenticate(cfg.ClearPassword)
if err != nil {
return nil, err
}
t := &Tor{
tc: tc,
tcmd: tcmd,
bc: bc,
}
return t, nil
}
// Tor is a higher level wrapper to a child tor process
type Tor struct {
tc *tor.Config
tcmd *tor.Cmd
bc *bulb.Conn
}
// AddOnion adds an onion service to this machine with the given private key
// (can be nil for an auto-generated key), virtual onion port and TCP destunation.
func (t *Tor) AddOnion(pKey *rsa.PrivateKey, virtPort uint16, destination string) (*bulb.OnionInfo, error) {
return t.bc.AddOnion([]bulb.OnionPortSpec{
{
VirtPort: virtPort,
Target: destination,
},
}, pKey, false)
}

29
utils/crypto.go Normal file
View File

@ -0,0 +1,29 @@
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
// RSAPrivateKeyToPem takes an RSA private key and formats it in PEM-encoded bytes.
func RSAPrivateKeyToPem(k *rsa.PrivateKey) []byte {
pemblock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k),
}
return pem.EncodeToMemory(pemblock)
}
// PemToRSAPrivateKey takes a PEM-encoded rsa private key and returns it.
func PemToRSAPrivateKey(data []byte) (*rsa.PrivateKey, error) {
var block *pem.Block
block, _ = pem.Decode([]byte(data))
pKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return pKey, nil
}