Browse Source

fixing ConnWithMetrics, TunneledOutboundSOCKS5 forward proxy appears to

be working now
forward-proxy
forest 2 months ago
parent
commit
2aa40ea2e9
5 changed files with 46 additions and 15 deletions
  1. +1
    -0
      .gitignore
  2. +3
    -1
      main_client.go
  3. +1
    -1
      main_server.go
  4. +4
    -0
      tunnel-lib/server.go
  5. +37
    -13
      tunnel-lib/util.go

+ 1
- 0
.gitignore View File

@ -1,3 +1,4 @@
build
dockerbuild
localbuild.sh
config.json

+ 3
- 1
main_client.go View File

@ -409,7 +409,7 @@ func runClient(configFileName *string) {
if config.MaximumConnectionRetrySeconds != 0 {
maximumConnectionRetrySeconds = config.MaximumConnectionRetrySeconds
}
for _, server := range clientServers {
for i, server := range clientServers {
// make a separate backoff instance for each server.
myBackoff := maximumBackoff{
Maximum: time.Second * time.Duration(maximumConnectionRetrySeconds),
@ -454,6 +454,7 @@ func runClient(configFileName *string) {
})()
server.Client = client
clientServers[i] = server
go server.Client.Start()
}
@ -474,6 +475,7 @@ func runClient(configFileName *string) {
}
// TODO better way of determining which one to use for forward proxy.
// log.Printf("clientServers: %+v, clientServers[0]: %+v\n", clientServers, clientServers[0])
err = clientServers[0].Client.HandleForwardProxy(conn)
if err != nil {
log.Printf("Can't accept incoming connection %s -> %s because %s\n", conn.RemoteAddr, conn.LocalAddr, err)


+ 1
- 1
main_server.go View File

@ -115,7 +115,7 @@ func runServer(configFileName *string) {
// the Server should only collect metrics when in multi-tenant mode -- this is needed for billing
if config.MultiTenantMode {
metricChannel = make(chan tunnel.BandwidthMetric)
metricChannel = make(chan tunnel.BandwidthMetric, 4096)
}
tunnelServerConfig := &tunnel.ServerConfig{


+ 4
- 0
tunnel-lib/server.go View File

@ -506,6 +506,7 @@ func (s *Server) listenControl(ct *control) {
}
if msg.Action == proto.RequestForwardProxy {
log.Printf("Server.listenControl(): connectionId: %d s.getSession(%s)\n", connectionId, ct.identifier)
session, err := s.getSession(ct.identifier)
if err != nil {
fmt.Printf(
@ -513,6 +514,7 @@ func (s *Server) listenControl(ct *control) {
connectionId, ct.identifier, err,
)
}
log.Printf("Server.listenControl(): connectionId: %d session.Open()\n", connectionId)
remote, err := session.Open()
if err != nil {
fmt.Printf(
@ -529,6 +531,8 @@ func (s *Server) listenControl(ct *control) {
inbound: false,
clientId: ct.identifier,
}
log.Printf("Server.listenControl(): connectionId: %d s.socks5Server.ServeConn(metricsWrappedConn)\n", connectionId)
err := s.socks5Server.ServeConn(metricsWrappedConn)
if err != nil {
fmt.Printf(


+ 37
- 13
tunnel-lib/util.go View File

@ -240,35 +240,59 @@ type ConnWithMetrics struct {
inbound bool
service string
clientId string
inboundBytes int
outboundBytes int
remoteAddress net.Addr
}
func (conn ConnWithMetrics) Read(b []byte) (n int, err error) {
n, err = conn.underlying.Read(b)
conn.metricsChannel <- BandwidthMetric{
Inbound: conn.inbound,
ClientId: conn.clientId,
RemoteAddress: conn.remoteAddress,
Service: conn.service,
Bytes: n,
}
conn.Accumulate(conn.inbound, n)
return n, err
}
func (conn ConnWithMetrics) Write(b []byte) (n int, err error) {
n, err = conn.underlying.Write(b)
conn.Accumulate(!conn.inbound, n)
return n, err
}
func (conn ConnWithMetrics) Close() error {
if conn.inboundBytes > 0 {
conn.PushMetric(true, conn.inboundBytes)
}
if conn.outboundBytes > 0 {
conn.PushMetric(false, conn.outboundBytes)
}
return conn.underlying.Close()
}
func (conn ConnWithMetrics) Accumulate(inbound bool, n int) {
if inbound {
conn.inboundBytes += n
if conn.inboundBytes > metricChunkSize {
conn.PushMetric(true, conn.inboundBytes)
conn.inboundBytes = 0
}
} else {
conn.outboundBytes += n
if conn.outboundBytes > metricChunkSize {
conn.PushMetric(false, conn.outboundBytes)
conn.outboundBytes = 0
}
}
}
func (conn ConnWithMetrics) PushMetric(inbound bool, n int) {
conn.metricsChannel <- BandwidthMetric{
Inbound: !conn.inbound,
Inbound: inbound,
ClientId: conn.clientId,
RemoteAddress: conn.remoteAddress,
Service: conn.service,
Bytes: n,
}
return n, err
}
func (conn ConnWithMetrics) Close() error {
return conn.underlying.Close()
}
func (conn ConnWithMetrics) LocalAddr() net.Addr {


Loading…
Cancel
Save