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

View File

@ -14,7 +14,7 @@ import (
import (
. "github.com/eyedeekay/httptunnel"
"github.com/eyedeekay/littleboss"
"crawshaw.io/littleboss"
)
var (
@ -22,6 +22,7 @@ var (
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")
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")
inboundTunnelLength = flag.Int("in-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)),
SetCloseIdle(*closeIdle),
SetCloseIdleTime(uint(*closeIdleTime)),
SetKeysPath(*destfile),
)
if err != nil {
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
func SetControlPort(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {

View File

@ -5,8 +5,10 @@ import (
"fmt"
"golang.org/x/time/rate"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
@ -17,7 +19,7 @@ import (
)
type SAMHTTPProxy struct {
gosam *goSam.Client
goSam *goSam.Client
client *http.Client
transport *http.Transport
rateLimiter *rate.Limiter
@ -26,6 +28,8 @@ type SAMHTTPProxy struct {
SamPort string
controlHost string
controlPort string
destination string
keyspath string
inLength uint
outLength uint
inVariance int
@ -51,7 +55,7 @@ type SAMHTTPProxy struct {
func (p *SAMHTTPProxy) freshTransport() *http.Transport {
t := http.Transport{
DialContext: p.gosam.DialContext,
DialContext: p.goSam.DialContext,
MaxConnsPerHost: 1,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 1,
@ -73,7 +77,7 @@ func (p *SAMHTTPProxy) freshClient() *http.Client {
}
func (p *SAMHTTPProxy) freshSAMClient() (*goSam.Client, error) {
return p.gosam.NewClient()
return p.goSam.NewClient()
}
//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) {
log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)
p.Save()
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
if !(req.Method == http.MethodConnect) {
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) {
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 {
http.Error(wr, err.Error(), http.StatusServiceUnavailable)
return
@ -171,7 +175,27 @@ func (p *SAMHTTPProxy) connect(wr http.ResponseWriter, req *http.Request) {
}
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) {
@ -197,14 +221,16 @@ func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
handler.useOutProxy = false
handler.compression = true
handler.id = 0
//handler.
handler.keyspath = "invalid.tunkey"
handler.destination = ""
for _, o := range opts {
if err := o(&handler); err != nil {
return nil, err
}
}
var err error
handler.gosam, err = goSam.NewClientFromOptions(
handler.destination = handler.Save()
handler.goSam, err = goSam.NewClientFromOptions(
goSam.SetHost(handler.SamHost),
goSam.SetPort(handler.SamPort),
goSam.SetUnpublished(handler.dontPublishLease),
@ -221,6 +247,7 @@ func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
goSam.SetCloseIdleTime(handler.closeIdleTime),
goSam.SetCompression(handler.compression),
goSam.SetDebug(handler.debug),
goSam.SetLocalDestination(handler.destination),
)
if err != nil {
return nil, err