Skip to content

Commit

Permalink
Refactor proxy connection handling, improve error logging, and update…
Browse files Browse the repository at this point in the history
… WebSocket URL to secure connection
  • Loading branch information
mojocn committed Nov 11, 2024
1 parent 31243c5 commit c24d3e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
58 changes: 24 additions & 34 deletions shadowos/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func handshake(conn net.Conn, uuidS string) (connData []byte, err error) {
}

func (ss *ShadowosApp) handleConnection(conn net.Conn) {
defer log.Print("request done -->>")
defer conn.Close()
connBytes, err := handshake(conn, ss.UUID)
if err != nil {
Expand Down Expand Up @@ -202,9 +201,7 @@ func (ps ProxySession) Close() error {

func (ps *ProxySession) doProxy(socks net.Conn) {
go func() {
// tk := time.NewTicker(time.Second * 5)
defer func() {
// tk.Stop()
ps.ch <- struct{}{}
}()
for {
Expand Down Expand Up @@ -242,44 +239,37 @@ func (ps *ProxySession) doProxy(socks net.Conn) {

go func() {
defer func() {
// tk.Stop()
ps.ch <- struct{}{}
}()
for {
select {

default:
buf := make([]byte, 1024)
n, err := socks.Read(buf)
if n > 0 {
log.Println("socks read N:", n)
if len(ps.connData) > 0 {
buf = append(ps.connData, buf[:n]...)
ps.connData = nil
} else {
buf = buf[:n]
}
err = ps.ws.WriteMessage(websocket.BinaryMessage, buf)
if err != nil {
log.Println("failed to write to websocket", err)
}

}
//socks5 EOF
if err != io.EOF {
continue
}
if err != net.ErrClosed {
log.Print("socks5 closed")
return
buf := make([]byte, 1024)
n, err := socks.Read(buf)
if n > 0 {
log.Println("socks read N:", n)
if len(ps.connData) > 0 {
buf = append(ps.connData, buf[:n]...)
ps.connData = nil
} else {
buf = buf[:n]
}

err = ps.ws.WriteMessage(websocket.BinaryMessage, buf)
if err != nil {
log.Printf("%T", err)
log.Println("failed to read from socks5 to websocket", err)
return
log.Println("failed to write to websocket", err)
}
}
//socks5 EOF
if err != io.EOF {
continue
}
if err != net.ErrClosed {
log.Print("socks5 closed")
return
}
if err != nil {
log.Printf("%T", err)
log.Println("failed to read from socks5 to websocket", err)
return
}
}
}()
<-ps.ch
Expand Down
2 changes: 1 addition & 1 deletion shadowos/man/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
url = "ws://127.0.0.1:8787/53881505-c10c-464a-8949-e57184a576a9"
url = "wss://demo.libragen.cn/53881505-c10c-464a-8949-e57184a576a9"
app = &shadowos.ShadowosApp{
AddrWs: url,
AddrSocks5: "127.0.0.1:1080",
Expand Down

0 comments on commit c24d3e2

Please sign in to comment.