implement the tunnel interface for sam-forwarder

This commit is contained in:
idk
2019-04-21 10:54:07 -04:00
parent d3ba6b47cb
commit 9c9accef5d
3 changed files with 110 additions and 7 deletions

View File

@ -84,6 +84,7 @@ func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
srv.Handler, err = NewHttpProxy(
SetHost(*samHostString),
SetPort(*samPortString),
SetProxyAddr(ln.Addr().String()),
SetControlAddr(cln.Addr().String()),
SetDebug(*debugConnection),
SetInLength(uint(*inboundTunnelLength)),
@ -156,9 +157,11 @@ func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
os.Setenv("http_proxy", "http://"+ln.Addr().String())
os.Setenv("https_proxy", "http://"+ln.Addr().String())
os.Setenv("ftp_proxy", "http://"+ln.Addr().String())
os.Setenv("HTTP_proxy", "http://"+ln.Addr().String())
os.Setenv("HTTPS_proxy", "http://"+ln.Addr().String())
os.Setenv("FTP_proxy", "http://"+ln.Addr().String())
os.Setenv("all_proxy", "http://"+ln.Addr().String())
os.Setenv("HTTP_PROXY", "http://"+ln.Addr().String())
os.Setenv("HTTPS_PROXY", "http://"+ln.Addr().String())
os.Setenv("FTP_PROXY", "http://"+ln.Addr().String())
os.Setenv("ALL_PROXY", "http://"+ln.Addr().String())
log.Println("Launching ", *runCommand, "with proxy http://"+ln.Addr().String())
cmd := exec.Command(*runCommand, strings.Split(*runArguments, " ")...)

View File

@ -75,6 +75,39 @@ func SetControlAddr(s ...string) func(*SAMHTTPProxy) error {
}
}
//SetProxyAddr sets a clients's address in the form host:port or host, port
func SetProxyAddr(s ...string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if len(s) == 1 {
split := strings.SplitN(s[0], ":", 2)
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.proxyHost = split[0]
c.proxyPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
}
return fmt.Errorf("Invalid port; non-number")
}
return fmt.Errorf("Invalid address; use host:port %s ", split)
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.proxyHost = s[0]
c.proxyPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
}
return fmt.Errorf("Invalid port; non-number")
} else {
return fmt.Errorf("Invalid address")
}
}
}
//SetAddrMixed sets a clients's address in the form host, port(int)
func SetAddrMixed(s string, i int) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {

View File

@ -7,8 +7,10 @@ import (
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
@ -24,6 +26,8 @@ type SAMHTTPProxy struct {
transport *http.Transport
rateLimiter *rate.Limiter
id int32
proxyHost string
proxyPort string
SamHost string
SamPort string
controlHost string
@ -61,6 +65,71 @@ func plog(in ...interface{}) {
}
}
func (p *SAMHTTPProxy) Cleanup() {
p.Close()
}
func (p *SAMHTTPProxy) Print() string {
return p.goSam.Print()
}
func (p *SAMHTTPProxy) Search(search string) string {
terms := strings.Split(search, ",")
if search == "" {
return p.Print()
}
for _, value := range terms {
if !strings.Contains(p.Print(), value) {
return ""
}
}
return p.Print()
}
func (p *SAMHTTPProxy) Target() string {
return p.proxyHost + ":" + p.proxyPort
}
func (p *SAMHTTPProxy) ID() string {
return strconv.Itoa(int(p.id))
}
func (p *SAMHTTPProxy) Base32() string {
return p.goSam.Base32()
}
func (p *SAMHTTPProxy) Base64() string {
return p.goSam.Base64()
}
func (p *SAMHTTPProxy) Serve() error {
ln, err := net.Listen("tcp", p.proxyHost+":"+p.proxyPort)
if err != nil {
return err
}
srv := &http.Server{
ReadTimeout: 600 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: ln.Addr().String(),
}
srv.Handler = p
if err != nil {
return err
}
log.Println("Starting proxy server on", ln.Addr())
if err := srv.Serve(ln); err != nil {
if err == http.ErrServerClosed {
return err
}
}
log.Println("Stopping proxy server on", ln.Addr())
return nil
}
func (p *SAMHTTPProxy) Close() error {
return p.goSam.Close()
}
func (p *SAMHTTPProxy) freshTransport() *http.Transport {
t := http.Transport{
DialContext: p.goSam.DialContext,
@ -192,10 +261,6 @@ func (p *SAMHTTPProxy) connect(wr http.ResponseWriter, req *http.Request) {
go proxycommon.Transfer(client_conn, dest_conn)
}
func (p *SAMHTTPProxy) Close() error {
return p.goSam.Close()
}
func (p *SAMHTTPProxy) Save() string {
if p.keyspath != "invalid.tunkey" {
if _, err := os.Stat(p.keyspath); os.IsNotExist(err) {
@ -222,6 +287,8 @@ func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
handler.SamPort = "7656"
handler.controlHost = "127.0.0.1"
handler.controlPort = "7951"
handler.proxyHost = "127.0.0.1"
handler.proxyPort = "7950"
handler.inLength = 2
handler.outLength = 2
handler.inVariance = 0