-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from firefart/dev
Dev
- Loading branch information
Showing
10 changed files
with
265 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"time" | ||
|
||
socks "github.com/firefart/gosocks" | ||
) | ||
|
||
func main() { | ||
log := &socks.NilLogger{} | ||
handler := MyCustomHandler{ | ||
Timeout: 1 * time.Second, | ||
PropA: "A", | ||
PropB: "B", | ||
Log: log, | ||
} | ||
p := socks.Proxy{ | ||
ServerAddr: "127.0.0.1:1080", | ||
Proxyhandler: &handler, | ||
Timeout: 1 * time.Second, | ||
Log: log, | ||
} | ||
log.Infof("starting SOCKS server on %s", p.ServerAddr) | ||
if err := p.Start(); err != nil { | ||
panic(err) | ||
} | ||
<-p.Done | ||
} | ||
|
||
type MyCustomHandler struct { | ||
Timeout time.Duration | ||
PropA string | ||
PropB string | ||
Log socks.Logger | ||
} | ||
|
||
func (s *MyCustomHandler) Init(request socks.Request) (io.ReadWriteCloser, *socks.Error) { | ||
target := fmt.Sprintf("%s:%d", request.DestinationAddress, request.DestinationPort) | ||
s.Log.Infof("Connecting to target %s", target) | ||
remote, err := net.DialTimeout("tcp", target, s.Timeout) | ||
if err != nil { | ||
return nil, socks.NewError(socks.RequestReplyNetworkUnreachable, err) | ||
} | ||
return remote, nil | ||
} | ||
|
||
func (s *MyCustomHandler) Refresh(ctx context.Context) { | ||
tick := time.NewTicker(10 * time.Second) | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-tick.C: | ||
s.Log.Debug("refreshing connection") | ||
} | ||
} | ||
|
||
func (s *MyCustomHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error { | ||
i, err := io.Copy(client, remote) | ||
if err != nil { | ||
return err | ||
} | ||
s.Log.Debugf("wrote %d bytes to client", i) | ||
return nil | ||
} | ||
|
||
func (s *MyCustomHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error { | ||
i, err := io.Copy(remote, client) | ||
if err != nil { | ||
return err | ||
} | ||
s.Log.Debugf("wrote %d bytes to remote", i) | ||
return nil | ||
} | ||
|
||
func (s MyCustomHandler) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
socks "github.com/firefart/gosocks" | ||
) | ||
|
||
func main() { | ||
handler := socks.DefaultHandler{ | ||
Timeout: 1 * time.Second, | ||
} | ||
listen := "127.0.0.1:1080" | ||
p := socks.Proxy{ | ||
ServerAddr: listen, | ||
Proxyhandler: handler, | ||
Timeout: 1 * time.Second, | ||
Log: &socks.NilLogger{}, | ||
} | ||
p.Log.Infof("starting SOCKS server on %s", listen) | ||
if err := p.Start(); err != nil { | ||
panic(err) | ||
} | ||
<-p.Done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/firefart/gosocks | ||
|
||
go 1.18 | ||
go 1.21 |
Oops, something went wrong.