2018-01-22 00:40:04 +08:00
|
|
|
// https://tools.ietf.org/html/rfc1928
|
|
|
|
|
2017-07-13 21:55:41 +08:00
|
|
|
// socks5 client:
|
|
|
|
// https://github.com/golang/net/tree/master/proxy
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-09-07 17:17:38 +08:00
|
|
|
// Package socks5 implements a socks5 proxy.
|
2018-06-26 16:15:48 +08:00
|
|
|
package socks5
|
2017-07-13 21:55:41 +08:00
|
|
|
|
|
|
|
import (
|
2018-06-26 16:15:48 +08:00
|
|
|
"net/url"
|
2017-07-13 21:55:41 +08:00
|
|
|
|
2020-10-01 22:49:14 +08:00
|
|
|
"github.com/nadoo/glider/log"
|
2018-08-12 12:37:25 +08:00
|
|
|
"github.com/nadoo/glider/proxy"
|
2017-07-13 21:55:41 +08:00
|
|
|
)
|
|
|
|
|
2019-03-18 23:37:01 +08:00
|
|
|
// Version is socks5 version number.
|
2018-06-26 16:15:48 +08:00
|
|
|
const Version = 5
|
2017-07-13 21:55:41 +08:00
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
// Socks5 is a base socks5 struct.
|
|
|
|
type Socks5 struct {
|
2018-08-12 12:37:25 +08:00
|
|
|
dialer proxy.Dialer
|
2019-09-18 19:40:14 +08:00
|
|
|
proxy proxy.Proxy
|
2018-03-24 19:57:46 +08:00
|
|
|
addr string
|
2017-07-29 23:20:27 +08:00
|
|
|
user string
|
|
|
|
password string
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-12 12:37:25 +08:00
|
|
|
func init() {
|
|
|
|
proxy.RegisterDialer("socks5", NewSocks5Dialer)
|
|
|
|
proxy.RegisterServer("socks5", NewSocks5Server)
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:00:46 +08:00
|
|
|
// NewSocks5 returns a Proxy that makes SOCKS v5 connections to the given address.
|
2019-03-18 23:37:01 +08:00
|
|
|
// with an optional username and password. (RFC 1928)
|
2019-09-18 19:40:14 +08:00
|
|
|
func NewSocks5(s string, d proxy.Dialer, p proxy.Proxy) (*Socks5, error) {
|
2018-06-26 16:15:48 +08:00
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
log.F("parse err: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := u.Host
|
2018-07-06 11:30:42 +08:00
|
|
|
user := u.User.Username()
|
|
|
|
pass, _ := u.User.Password()
|
2018-06-26 16:15:48 +08:00
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
h := &Socks5{
|
|
|
|
dialer: d,
|
|
|
|
proxy: p,
|
2018-03-24 19:57:46 +08:00
|
|
|
addr: addr,
|
|
|
|
user: user,
|
|
|
|
password: pass,
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
2018-06-26 16:15:48 +08:00
|
|
|
return h, nil
|
|
|
|
}
|