forked from firefart/gosocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaulthandler.go
46 lines (40 loc) · 1.21 KB
/
defaulthandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package socks
import (
"context"
"fmt"
"io"
"net"
"time"
)
// DefaultHandler is the default socks5 implementation
type DefaultHandler struct {
// Timeout defines the connect timeout to the destination
Timeout time.Duration
}
// PreHandler is the default socks5 implementation
func (s DefaultHandler) Init(addr net.Addr, request Request) (io.ReadWriteCloser, *Error) {
target := fmt.Sprintf("%s:%d", request.DestinationAddress, request.DestinationPort)
remote, err := net.DialTimeout("tcp", target, s.Timeout)
if err != nil {
return nil, NewError(RequestReplyNetworkUnreachable, err)
}
return remote, nil
}
// CopyFromClientToRemote is the default socks5 implementation
func (s DefaultHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error {
if _, err := io.Copy(remote, client); err != nil {
return err
}
return nil
}
// CopyFromRemoteToClient is the default socks5 implementation
func (s DefaultHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error {
if _, err := io.Copy(client, remote); err != nil {
return err
}
return nil
}
// Cleanup is the default socks5 implementation
func (s DefaultHandler) Close() error {
return nil
}