Skip to content

Commit

Permalink
fix proxy I broke when I added TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedeekay committed Aug 29, 2022
1 parent 3faeafb commit 1e14630
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LOG := log/
ETC := etc/
USR := usr/
LOCAL := local/
VERSION := 0.33.0
VERSION := 0.33.1

GO111MODULE=on

Expand Down
2 changes: 1 addition & 1 deletion config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

import (
sfi2pkeys "github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/i2pkeys"
sfi2pkeys "github.com/eyedeekay/sam-forwarder/i2pkeys"
)

var err error
Expand Down
4 changes: 4 additions & 0 deletions config/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package i2ptunconf

import (
"log"
"strings"
)

Expand All @@ -19,12 +20,15 @@ func (c *Conf) GetTypes(argc, argu, argh bool, def string, label ...string) stri
}
typ += "client"
c.Client = true
c.Type = typ
} else {
if argh == true {
typ += "http"
} else {
typ += "server"
}
log.Printf("TUNNEL TYPE\n\t%s\n", typ)
c.Type = typ
if typ != def {
return typ
}
Expand Down
42 changes: 21 additions & 21 deletions etc/samcatd/tunnels.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ i2cp.enableWhiteList = false
i2cp.enableBlackList = false
#keyfile = "/usr/share/samcatd/samcatd"

#[sam-forwarder-tcp-server]
#type = server
#host = 127.0.0.1
#port = 7669
#inbound.length = 2
#outbound.length = 2
#keys = tcpserver

#[sam-forwarder-tcp-client]
#type = client
#host = 127.0.0.1
#port = 8082
#inbound.length = 2
#outbound.length = 3
#destination = i2p-projekt.i2p
#keys = tcpclient
[sam-forwarder-tcp-server]
type = http
host = 127.0.0.1
port = 7880
inbound.length = 2
outbound.length = 2
keys = tcpserver

[sam-forwarder-udp-server]
type = udpserver
[sam-forwarder-tcp-client]
type = client
host = 127.0.0.1
port = 8083
inbound.length = 3
port = 8082
inbound.length = 2
outbound.length = 3
keys = udpserver-example
destination = i2p-projekt.i2p
keys = tcpclient

#[sam-forwarder-udp-server]
#type = udpserver
#host = 127.0.0.1
#port = 8083
#inbound.length = 3
#outbound.length = 3
#keys = udpserver-example

#[sam-forwarder-udp-client]
#type = udpclient
Expand Down
2 changes: 1 addition & 1 deletion i2pkeys/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/eyedeekay/sam-forwarder/i2pkeys/keys"
//"github.com/eyedeekay/sam-forwarder/i2pkeys/password"
"github.com/eyedeekay/sam3"
"github.com/eyedeekay/i2pkeys"
"github.com/eyedeekay/sam3"
)

func Encrypt(i2pkeypath, aeskeypath string) error {
Expand Down
2 changes: 1 addition & 1 deletion interface/interface.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package samtunnel

import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/i2pkeys"
"github.com/eyedeekay/sam-forwarder/config"
)

// SAMTunnel is an interface comprehensively representing an I2P tunnel over SAM
Expand Down
2 changes: 1 addition & 1 deletion tcp/forwarder-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
sfi2pkeys "github.com/eyedeekay/sam-forwarder/i2pkeys"
samtunnel "github.com/eyedeekay/sam-forwarder/interface"

"github.com/eyedeekay/i2pkeys"
samoptions "github.com/eyedeekay/sam-forwarder/options"
"github.com/eyedeekay/sam3"
"github.com/eyedeekay/i2pkeys"
)

// SAMClientForwarder is a tcp proxy that automatically forwards ports to i2p
Expand Down
110 changes: 71 additions & 39 deletions tcp/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,61 +241,93 @@ func (f *SAMForwarder) forward(conn net.Conn) { //(conn net.Conn) {
if !f.Up() {
return
}
var request *http.Request
var requestbytes []byte
var responsebytes []byte
var request *http.Request
//var responsebytes []byte
log.Println("Proxy type:", f.Conf.Type)
go func() {
var err error
var client net.Conn
if client, err = net.Dial("tcp", f.Target()); err != nil {
if client, err := net.Dial("tcp", f.Target()); err != nil {
log.Fatalf("Dial failed: %v", err)
}
defer client.Close()
if f.Conf.Type == "http" || f.Conf.Type == "https" {
if requestbytes, request, err = f.HTTPRequestBytes(conn); err == nil {
log.Printf("Forwarding modified request: \n\t %s", string(requestbytes))
client.Write(requestbytes)
} else {
log.Println("Error: ", requestbytes, err)
}
} else {
if f.ByteLimit > 0 {
if val, ok := f.Bytes[f.ClientBase64(conn)]; ok == true {
if val > f.ByteLimit {
return
defer client.Close()
if f.Conf.Type == "http" || f.Conf.Type == "https" {
if requestbytes, request, err = f.HTTPRequestBytes(conn); err == nil {
log.Printf("Forwarding modified request: \n\t %s", string(requestbytes))
client.Write(requestbytes)
} else {
log.Println("Error: ", requestbytes, err)
}
if responsebytes, err := f.HTTPResponseBytes(client, request); err == nil {
log.Printf("Forwarding modified response: \n\t%s", string(responsebytes))
conn.Write(responsebytes)
} else {
log.Println("Response Error: ", responsebytes, err)
}
} else {
if f.ByteLimit > 0 {
log.Printf("Forwarding unmodified request: \n\t")
if val, ok := f.Bytes[f.ClientBase64(conn)]; ok {
if val > f.ByteLimit {
log.Printf("Transfer limit reached")
return
}
}
}
}
if count, err := io.Copy(client, conn); err == nil {
if f.ByteLimit > 0 {
f.Bytes[f.ClientBase64(conn)] += count
log.Printf("Forwarding unmodified response: \n\t")
if val, ok := f.Bytes[f.ClientBase64(conn)]; ok == true {
if val > f.ByteLimit {
return
}
}
}
Proxy(client, conn)
}
}
}()
go func() {
var err error
var client net.Conn
if client, err = net.Dial("tcp", f.Target()); err != nil {
log.Fatalf("Dial failed: %v", err)

/*} else {
log.Printf("Forwarding unmodified request: \n\t")
if f.ByteLimit > 0 {
if val, ok := f.Bytes[f.ClientBase64(conn)]; ok {
if val > f.ByteLimit {
log.Printf("Transfer limit reached")
return
}
}
}
if count, err := io.Copy(client, conn); err == nil {
log.Println("copied", count, "bytes from", client.RemoteAddr().String(), "to", conn.RemoteAddr().String())
if f.ByteLimit > 0 {
f.Bytes[f.ClientBase64(conn)] += count
}
} else {
log.Println(err)
}
defer client.Close()
if f.Conf.Type == "http" || f.Conf.Type == "https" {
if responsebytes, err = f.HTTPResponseBytes(client, request); err == nil {
}*/
//}()
/*go func() {
//var err error
//var client net.Conn
if client, err := net.Dial("tcp", f.Target()); err != nil {
log.Fatalf("Dial failed: %v", err)
} else {
defer client.Close()
//if f.Conf.Type == "http" || f.Conf.Type == "https" {
if responsebytes, err := f.HTTPResponseBytes(client, request); err == nil {
log.Printf("Forwarding modified response: \n\t%s", string(responsebytes))
conn.Write(responsebytes)
} else {
log.Println("Response Error: ", responsebytes, err)
}
} else {
if val, ok := f.Bytes[f.ClientBase64(conn)]; ok == true {
if val > f.ByteLimit {
return
}
log.Println("Response Error: ", responsebytes, err)log.Printf("Forwarding unmodified request: \n\t")
}
io.Copy(conn, client)
}
}()
}()*/
//}
/* else {
}*/
//}()
}

//Base32 returns the base32 address where the local service is being forwarded
Expand Down Expand Up @@ -348,7 +380,7 @@ func (f *SAMForwarder) Serve() error {
}
defer conn.Close()
log.Printf("Accepted connection %v\n", conn)
go f.forward(conn)
f.forward(conn)
}
}
return nil
Expand Down
62 changes: 62 additions & 0 deletions tcp/tcpproxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package samforwarder

import (
"io"
"log"
"net"
)

/*
Adapted from: https://gist.github.com/jbardin/821d08cb64c01c84b81a
*/

func Proxy(srvConn, cliConn net.Conn) {
// channels to wait on the close event for each connection
serverClosed := make(chan struct{}, 1)
clientClosed := make(chan struct{}, 1)

go broker(srvConn, cliConn, clientClosed)
go broker(cliConn, srvConn, serverClosed)

// wait for one half of the proxy to exit, then trigger a shutdown of the
// other half by calling CloseRead(). This will break the read loop in the
// broker and allow us to fully close the connection cleanly without a
// "use of closed network connection" error.
var waitFor chan struct{}
select {
case <-clientClosed:
// the client closed first and any more packets from the server aren't
// useful, so we can optionally SetLinger(0) here to recycle the port
// faster.
// srvConn.SetLinger(0)
// srvConn.CloseRead()
waitFor = serverClosed
case <-serverClosed:
// cliConn.CloseRead()
waitFor = clientClosed
}

// Wait for the other connection to close.
// This "waitFor" pattern isn't required, but gives us a way to track the
// connection and ensure all copies terminate correctly; we can trigger
// stats on entry and deferred exit of this function.
<-waitFor
}

// This does the actual data transfer.
// The broker only closes the Read side.
func broker(dst, src net.Conn, srcClosed chan struct{}) {
// We can handle errors in a finer-grained manner by inlining io.Copy (it's
// simple, and we drop the ReaderFrom or WriterTo checks for
// net.Conn->net.Conn transfers, which aren't needed). This would also let
// us adjust buffersize.
_, err := io.Copy(dst, src)

if err != nil {
log.Printf("Copy error: %s", err)
}
if err := src.Close(); err != nil {
log.Printf("Close error: %s", err)
}
srcClosed <- struct{}{}
}
2 changes: 1 addition & 1 deletion udp/forwarder-udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
sfi2pkeys "github.com/eyedeekay/sam-forwarder/i2pkeys"
samoptions "github.com/eyedeekay/sam-forwarder/options"

"github.com/eyedeekay/i2pkeys"
samtunnel "github.com/eyedeekay/sam-forwarder/interface"
"github.com/eyedeekay/sam3"
"github.com/eyedeekay/i2pkeys"
)

//SAMDGForwarder is a structure which automatically configured the forwarding of
Expand Down

0 comments on commit 1e14630

Please sign in to comment.