Skip to content

Commit

Permalink
Merge pull request #1 from Iam54r1n4/fix-proxy
Browse files Browse the repository at this point in the history
Fix proxy bug
  • Loading branch information
hiddify-com authored May 31, 2024
2 parents 1f48dbb + 61e8da8 commit 1f34745
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ func directTCPIPClosure(rdb *redis.Client) ssh.ChannelHandler {
}

dest := ipAddr.String()

if srv.LocalPortForwardingCallback == nil || !srv.LocalPortForwardingCallback(ctx, dest, d.DestPort) {
newChan.Reject(gossh.Prohibited, "illegal address")
return
}
force_direct := srv.LocalPortForwardingCallback != nil && srv.LocalPortForwardingCallback(ctx, dest, d.DestPort)
// Shouldn't use proxy if the destination ip is local
force_direct := srv.LocalPortForwardingCallback != nil && !srv.LocalPortForwardingCallback(ctx, dest, d.DestPort)
dest = net.JoinHostPort(dest, strconv.FormatInt(int64(d.DestPort), 10))

var dialer net.Dialer
var dconn net.Conn

if len(SocksProxyAddr) != 0 && !force_direct {
if len(SocksProxyAddr) != 0 && force_direct {
pDialer, err := proxy.SOCKS5("tcp", SocksProxyAddr, nil, proxy.Direct)
if err != nil {
newChan.Reject(gossh.ConnectionFailed, err.Error())
Expand Down Expand Up @@ -255,9 +256,9 @@ func main() {
result := rdb.SIsMember(ctx, "ssh-server:users", userString)
res, err := result.Result()
doneCh := ctx.Done()
//log.Printf("UserString -%s- res -%s- err -%s-", userString,res,err)
//log.Printf("UserString -%s- res -%s- err -%s-", userString,res,err)
if err != nil || !res || doneCh == nil {
//log.Printf("returning false 1")
//log.Printf("returning false 1")
return false
}
userConnectionCountMutex.Lock()
Expand All @@ -268,21 +269,21 @@ func main() {
connCntStr, _ := hget_res.Result()
connCnt, err2 := strconv.ParseInt(connCntStr, 10, 32)
if err2 == nil && connCnt >= maxConns {
//log.Printf("returning false 2")
//log.Printf("returning false 2")
//log.Printf("Client %s trying to have more than %d connections\n", userString, maxConns)
return false // No duplicate connections
}
hincr_res := rdb.HIncrBy(ctx, "ssh-server:connections", userId, 1)
if hincr_res.Err() != nil {
//log.Printf("returning false 3 %s",hincr_res.Err())
//log.Printf("returning false 3 %s",hincr_res.Err())
return false
}
go func() {
<-doneCh
//log.Printf("4---",userId)
//log.Printf("4---",userId)
rdb.HIncrBy(context.Background(), "ssh-server:connections", userId, -1)
}()
//log.Printf("returning true ")
//log.Printf("returning true ")
return true
},
IdleTimeout: time.Minute * 1,
Expand Down

0 comments on commit 1f34745

Please sign in to comment.