-
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.
- Loading branch information
Showing
11 changed files
with
291 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# ipt2socks | ||
将iptables(TPROXY)透明代理流量转换为socks5流量的golang版工具,类似工具有[zfl9/ipt2socks](https://github.com/zfl9/ipt2socks) | ||
|
||
## 特性 | ||
* 支持转成sock4流量 | ||
* 支持转成socks5(tcp&udp)流量 | ||
|
||
## 使用 | ||
``` | ||
ipt2socks --listen=0.0.0.0:60080 --proxy=socks5://127.0.0.1:1080 | ||
``` | ||
|
||
### 参数 | ||
``` | ||
--listen 本地监听地址,格式为x.x.x.x:xx | ||
--proxy sock5代理地址,格式为sock5://x.x.x.x:xx或sock4://x.x.x.x:xx | ||
--udptimeout udp超时时间(单位秒) | ||
--verbose 若指定此选项,则将会打印较为详尽的运行时日志 | ||
``` | ||
|
||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package ipt2socks | ||
|
||
type Config struct { | ||
ProxyAddr string | ||
Proxy string | ||
ListenAddr string | ||
UDPTimeout int32 | ||
} |
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,62 @@ | ||
package ipt2socks | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/0990/socks5" | ||
"net" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type proxyDialer interface { | ||
Dial(network, addr string) (net.Conn, error) | ||
} | ||
|
||
func newProxyDialer(proxy string, udpTimeout int32) (proxyDialer, error) { | ||
protocol, addr, err := parseProxy(proxy) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch protocol { | ||
case "socks5": | ||
return socks5.NewSocks5Client(socks5.ClientCfg{ | ||
ServerAddr: addr, | ||
UserName: "", | ||
Password: "", | ||
UDPTimout: int(udpTimeout), | ||
TCPTimeout: 60, | ||
}), nil | ||
case "socks4": | ||
return socks5.NewSocks4Client(socks5.ClientCfg{ | ||
ServerAddr: addr, | ||
UserName: "", | ||
Password: "", | ||
UDPTimout: int(udpTimeout), | ||
TCPTimeout: 60, | ||
}), nil | ||
default: | ||
return nil, errors.New("not support proxy type") | ||
} | ||
} | ||
|
||
func parseProxy(s string) (string, string, error) { | ||
if !strings.Contains(s, "://") { | ||
s = fmt.Sprintf("%s://%s", "socks5" /* default protocol */, s) | ||
} | ||
|
||
u, err := url.Parse(s) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
protocol := strings.ToLower(u.Scheme) | ||
|
||
switch protocol { | ||
case "socks5", "socks4": | ||
return protocol, u.Host, nil | ||
default: | ||
return "", "", fmt.Errorf("unsupported protocol: %s", protocol) | ||
} | ||
} |
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
Oops, something went wrong.