Add SOCKS5 proxy support

This commit is contained in:
Ken-Håvard Lieng 2020-06-16 03:04:27 +02:00
parent 7040f1c8d0
commit 67fe5d263d
18 changed files with 1035 additions and 54 deletions

View file

@ -33,6 +33,8 @@ type Config struct {
Source string
HandleNickInUse func(string) string
Dialer Dialer
}
type Client struct {
@ -56,7 +58,7 @@ type Client struct {
conn net.Conn
connected bool
registered bool
dialer *net.Dialer
dialer Dialer
recvBuf []byte
scan *bufio.Scanner
backoff *backoff.Backoff
@ -89,6 +91,10 @@ func NewClient(config *Config) *Client {
config.SASLMechanisms = DefaultSASLMechanisms
}
if config.Dialer == nil {
config.Dialer = DefaultDialer
}
client := &Client{
Config: config,
Messages: make(chan *Message, 32),
@ -97,7 +103,7 @@ func NewClient(config *Config) *Client {
nick: config.Nick,
requestedCapabilities: map[string][]string{},
enabledCapabilities: map[string][]string{},
dialer: &net.Dialer{Timeout: 10 * time.Second},
dialer: config.Dialer,
recvBuf: make([]byte, 0, 4096),
backoff: &backoff.Backoff{
Min: 500 * time.Millisecond,

View file

@ -12,9 +12,15 @@ import (
)
var (
DefaultDialer = &net.Dialer{Timeout: 10 * time.Second}
ErrBadProtocol = errors.New("This server does not speak IRC")
)
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}
func (c *Client) Connect() {
c.connChange(false, nil)
go c.run()
@ -119,23 +125,23 @@ func (c *Client) connect() error {
c.lock.Lock()
defer c.lock.Unlock()
addr := net.JoinHostPort(c.Config.Host, c.Config.Port)
if c.Config.TLS {
conn, err := tls.DialWithDialer(c.dialer, "tcp", addr, c.Config.TLSConfig)
if err != nil {
return err
}
c.conn = conn
} else {
conn, err := c.dialer.Dial("tcp", addr)
if err != nil {
return err
}
c.conn = conn
conn, err := c.dialer.Dial("tcp", net.JoinHostPort(c.Config.Host, c.Config.Port))
if err != nil {
return err
}
if c.Config.TLS {
c.Config.TLSConfig.ServerName = c.Config.Host
tlsConn := tls.Client(conn, c.Config.TLSConfig)
err = tlsConn.Handshake()
if err != nil {
return err
}
conn = tlsConn
}
c.conn = conn
c.connected = true
c.connChange(true, nil)
c.scan = bufio.NewScanner(c.conn)

View file

@ -17,9 +17,11 @@ type DCCSend struct {
IP string `json:"ip"`
Port string `json:"port"`
Length uint64 `json:"length"`
dialer Dialer
}
func ParseDCCSend(ctcp *CTCP) *DCCSend {
func (c *Client) ParseDCCSend(ctcp *CTCP) *DCCSend {
params := strings.Split(ctcp.Params, " ")
if len(params) > 4 {
@ -43,20 +45,21 @@ func ParseDCCSend(ctcp *CTCP) *DCCSend {
IP: intToIP(ip),
Port: params[3],
Length: length,
dialer: c.dialer,
}
}
return nil
}
func DownloadDCC(w io.Writer, pack *DCCSend, progress chan DownloadProgress) error {
func (pack *DCCSend) Download(w io.Writer, progress chan DownloadProgress) error {
if progress != nil {
progress <- DownloadProgress{
File: pack.File,
}
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(pack.IP, pack.Port), 10*time.Second)
conn, err := pack.dialer.Dial("tcp", net.JoinHostPort(pack.IP, pack.Port))
if err != nil {
return err
}