switch to real littleboss

This commit is contained in:
idk
2019-03-16 23:40:59 -04:00
parent b8e4e915bf
commit e458f8737b
4 changed files with 60 additions and 23 deletions

View File

@ -12,7 +12,7 @@ import (
// tutorial line 48 // tutorial line 48
import ( import (
"github.com/eyedeekay/goSam" "github.com/eyedeekay/gosam"
) )
func copyHeader(dst, src http.Header) { func copyHeader(dst, src http.Header) {
@ -50,7 +50,7 @@ func delHopHeaders(header http.Header) {
// tutorial line 55 // tutorial line 55
type Proxy struct { type Proxy struct {
Sam *goSam.Client Sam *gosam.Client
Client *http.Client Client *http.Client
} }
@ -112,18 +112,18 @@ func main() {
flag.Parse() flag.Parse()
// tutorial line 71, 222 // tutorial line 71, 222
sam, err := goSam.NewClientFromOptions( sam, err := gosam.NewClientFromOptions(
goSam.SetHost("127.0.0.1"), gosam.SetHost("127.0.0.1"),
goSam.SetPort("7656"), gosam.SetPort("7656"),
goSam.SetUnpublished(true), gosam.SetUnpublished(true),
goSam.SetInLength(uint(2)), gosam.SetInLength(uint(2)),
goSam.SetOutLength(uint(2)), gosam.SetOutLength(uint(2)),
goSam.SetInQuantity(uint(1)), gosam.SetInQuantity(uint(1)),
goSam.SetOutQuantity(uint(1)), gosam.SetOutQuantity(uint(1)),
goSam.SetInBackups(uint(1)), gosam.SetInBackups(uint(1)),
goSam.SetOutBackups(uint(1)), gosam.SetOutBackups(uint(1)),
goSam.SetReduceIdle(true), gosam.SetReduceIdle(true),
goSam.SetReduceIdleTime(uint(2000000)), gosam.SetReduceIdleTime(uint(2000000)),
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -14,7 +14,7 @@ import (
import ( import (
. "github.com/eyedeekay/httptunnel" . "github.com/eyedeekay/httptunnel"
"github.com/eyedeekay/littleboss" "crawshaw.io/littleboss"
) )
var ( var (
@ -22,6 +22,7 @@ var (
samHostString = flag.String("bridge-host", "127.0.0.1", "host: of the SAM bridge") samHostString = flag.String("bridge-host", "127.0.0.1", "host: of the SAM bridge")
samPortString = flag.String("bridge-port", "7656", ":port of the SAM bridge") samPortString = flag.String("bridge-port", "7656", ":port of the SAM bridge")
watchProfiles = flag.String("watch-profiles", "~/.mozilla/.firefox.profile.i2p.default/user.js,~/.mozilla/.firefox.profile.i2p.debug/user.js", "Monitor and control these Firefox profiles") watchProfiles = flag.String("watch-profiles", "~/.mozilla/.firefox.profile.i2p.default/user.js,~/.mozilla/.firefox.profile.i2p.debug/user.js", "Monitor and control these Firefox profiles")
destfile = flag.String("dest-file", "invalid.tunkey", "Use a long-term destination key")
debugConnection = flag.Bool("conn-debug", false, "Print connection debug info") debugConnection = flag.Bool("conn-debug", false, "Print connection debug info")
inboundTunnelLength = flag.Int("in-tun-length", 2, "Tunnel Length(default 3)") inboundTunnelLength = flag.Int("in-tun-length", 2, "Tunnel Length(default 3)")
outboundTunnelLength = flag.Int("out-tun-length", 2, "Tunnel Length(default 3)") outboundTunnelLength = flag.Int("out-tun-length", 2, "Tunnel Length(default 3)")
@ -83,6 +84,7 @@ func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
SetReduceIdleQuantity(uint(*reduceIdleQuantity)), SetReduceIdleQuantity(uint(*reduceIdleQuantity)),
SetCloseIdle(*closeIdle), SetCloseIdle(*closeIdle),
SetCloseIdleTime(uint(*closeIdleTime)), SetCloseIdleTime(uint(*closeIdleTime)),
SetKeysPath(*destfile),
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -95,6 +95,14 @@ func SetControlHost(s string) func(*SAMHTTPProxy) error {
} }
} }
//SetKeysPath sets the path to the key save files
func SetKeysPath(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.keyspath = s
return nil
}
}
//SetContrlPort sets the host of the client's Proxy controller //SetContrlPort sets the host of the client's Proxy controller
func SetControlPort(s string) func(*SAMHTTPProxy) error { func SetControlPort(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error { return func(c *SAMHTTPProxy) error {

View File

@ -5,8 +5,10 @@ import (
"fmt" "fmt"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
) )
@ -17,7 +19,7 @@ import (
) )
type SAMHTTPProxy struct { type SAMHTTPProxy struct {
gosam *goSam.Client goSam *goSam.Client
client *http.Client client *http.Client
transport *http.Transport transport *http.Transport
rateLimiter *rate.Limiter rateLimiter *rate.Limiter
@ -26,6 +28,8 @@ type SAMHTTPProxy struct {
SamPort string SamPort string
controlHost string controlHost string
controlPort string controlPort string
destination string
keyspath string
inLength uint inLength uint
outLength uint outLength uint
inVariance int inVariance int
@ -51,7 +55,7 @@ type SAMHTTPProxy struct {
func (p *SAMHTTPProxy) freshTransport() *http.Transport { func (p *SAMHTTPProxy) freshTransport() *http.Transport {
t := http.Transport{ t := http.Transport{
DialContext: p.gosam.DialContext, DialContext: p.goSam.DialContext,
MaxConnsPerHost: 1, MaxConnsPerHost: 1,
MaxIdleConns: 0, MaxIdleConns: 0,
MaxIdleConnsPerHost: 1, MaxIdleConnsPerHost: 1,
@ -73,7 +77,7 @@ func (p *SAMHTTPProxy) freshClient() *http.Client {
} }
func (p *SAMHTTPProxy) freshSAMClient() (*goSam.Client, error) { func (p *SAMHTTPProxy) freshSAMClient() (*goSam.Client, error) {
return p.gosam.NewClient() return p.goSam.NewClient()
} }
//return the combined host:port of the SAM bridge //return the combined host:port of the SAM bridge
@ -83,7 +87,7 @@ func (p *SAMHTTPProxy) samaddr() string {
func (p *SAMHTTPProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) { func (p *SAMHTTPProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL) log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)
p.Save()
if req.URL.Scheme != "http" && req.URL.Scheme != "https" { if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
if !(req.Method == http.MethodConnect) { if !(req.Method == http.MethodConnect) {
msg := "Unsupported protocol scheme " + req.URL.Scheme msg := "Unsupported protocol scheme " + req.URL.Scheme
@ -150,7 +154,7 @@ func (p *SAMHTTPProxy) get(wr http.ResponseWriter, req *http.Request) {
func (p *SAMHTTPProxy) connect(wr http.ResponseWriter, req *http.Request) { func (p *SAMHTTPProxy) connect(wr http.ResponseWriter, req *http.Request) {
log.Println("CONNECT via i2p to", req.URL.Host) log.Println("CONNECT via i2p to", req.URL.Host)
dest_conn, err := p.gosam.Dial("tcp", req.URL.Host) dest_conn, err := p.goSam.Dial("tcp", req.URL.Host)
if err != nil { if err != nil {
http.Error(wr, err.Error(), http.StatusServiceUnavailable) http.Error(wr, err.Error(), http.StatusServiceUnavailable)
return return
@ -171,7 +175,27 @@ func (p *SAMHTTPProxy) connect(wr http.ResponseWriter, req *http.Request) {
} }
func (p *SAMHTTPProxy) Close() error { func (p *SAMHTTPProxy) Close() error {
return p.gosam.Close() return p.goSam.Close()
}
func (p *SAMHTTPProxy) Save() string {
if p.keyspath != "invalid.tunkey" {
if _, err := os.Stat(p.keyspath); os.IsNotExist(err) {
if p.goSam != nil {
if p.goSam.Destination() != "" {
ioutil.WriteFile(p.keyspath, []byte(p.goSam.Destination()), 0644)
p.destination = p.goSam.Destination()
return p.goSam.Destination()
}
}
} else {
if keys, err := ioutil.ReadFile(p.keyspath); err == nil {
p.destination = string(keys)
return string(keys)
}
}
}
return ""
} }
func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) { func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
@ -197,14 +221,16 @@ func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
handler.useOutProxy = false handler.useOutProxy = false
handler.compression = true handler.compression = true
handler.id = 0 handler.id = 0
//handler. handler.keyspath = "invalid.tunkey"
handler.destination = ""
for _, o := range opts { for _, o := range opts {
if err := o(&handler); err != nil { if err := o(&handler); err != nil {
return nil, err return nil, err
} }
} }
var err error var err error
handler.gosam, err = goSam.NewClientFromOptions( handler.destination = handler.Save()
handler.goSam, err = goSam.NewClientFromOptions(
goSam.SetHost(handler.SamHost), goSam.SetHost(handler.SamHost),
goSam.SetPort(handler.SamPort), goSam.SetPort(handler.SamPort),
goSam.SetUnpublished(handler.dontPublishLease), goSam.SetUnpublished(handler.dontPublishLease),
@ -221,6 +247,7 @@ func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
goSam.SetCloseIdleTime(handler.closeIdleTime), goSam.SetCloseIdleTime(handler.closeIdleTime),
goSam.SetCompression(handler.compression), goSam.SetCompression(handler.compression),
goSam.SetDebug(handler.debug), goSam.SetDebug(handler.debug),
goSam.SetLocalDestination(handler.destination),
) )
if err != nil { if err != nil {
return nil, err return nil, err