mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
修订代码, 默认loglevel 改为 Log_info.
对一般用户而言,还是需要使用Info等级 来了解一下 一般的 日志情况,等到使用熟练之后,且确认运行没有错误后, 可以自行调为 warning 来提升性能 发现 bubble包 还自己引入了 命令行参数,这十分不可取,所以我们还是直接使用其代码。 将其它包中 的 命令行参数 统一 移动 到 cmd/verysimple 中;tls lazy 特性因为还在 调试阶段,所以 命令行参数 仍然放到 v2ray_simple 包中。
This commit is contained in:
@@ -2,7 +2,6 @@ package netLayer
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"flag"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
@@ -16,14 +15,9 @@ var (
|
||||
the_geoipdb *maxminddb.Reader
|
||||
embedGeoip bool
|
||||
|
||||
GeoipFileName string //若运行程序指定了 geoip 参数,则该值为给定值;否则默认会被init为 GeoLite2-Country.mmdb
|
||||
GeoipFileName string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&GeoipFileName, "geoip", "GeoLite2-Country.mmdb", "geoip maxmind file name")
|
||||
|
||||
}
|
||||
|
||||
func HasEmbedGeoip() bool {
|
||||
return embedGeoip
|
||||
}
|
||||
@@ -42,7 +36,7 @@ func LoadMaxmindGeoipFile(fn string) {
|
||||
if fn == "" {
|
||||
fn = GeoipFileName
|
||||
}
|
||||
if fn == "" { //因为 GeoipFileName 是共有变量,所以可能会被设成"", 不排除脑残
|
||||
if fn == "" { //因为 GeoipFileName 是公有变量,所以可能会被设成""
|
||||
return
|
||||
}
|
||||
bs, e := os.ReadFile(fn)
|
||||
|
||||
@@ -10,12 +10,15 @@ import (
|
||||
/* go test -bench "CheckMMDB_country" . -v
|
||||
BenchmarkCheckMMDB_country-8 3631854 315.3 ns/op
|
||||
|
||||
总之一次mmdb查询比map查询慢了十倍多 (见 utils/container_test.go.bak)
|
||||
总之一次mmdb查询比map查询慢了十倍多 (见 以前代码的 utils/container_test.go.bak, 新代码已经删掉了,可以找老 tag 找到。)
|
||||
|
||||
有必要设置一个 国别-ip 的map缓存; 不过这种纳秒级别的优化就无所谓了; 也不好说,谁知道客户端的cpu有多垃圾
|
||||
*/
|
||||
|
||||
func BenchmarkCheckMMDB_country(b *testing.B) {
|
||||
|
||||
GeoipFileName = "GeoLite2-Country.mmdb"
|
||||
|
||||
b.StopTimer()
|
||||
b.ResetTimer()
|
||||
LoadMaxmindGeoipFile(utils.GetFilePath("../" + GeoipFileName))
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package netLayer
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
@@ -29,7 +28,6 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&UseReadv, "readv", DefaultReadvOption, "toggle the use of 'readv' syscall")
|
||||
|
||||
readvPool = sync.Pool{
|
||||
New: newReadvMem,
|
||||
|
||||
@@ -1,3 +1,97 @@
|
||||
/*
|
||||
Package tproxy implements tproxy.
|
||||
|
||||
透明代理只能用于linux。
|
||||
|
||||
About TProxy 关于透明代理
|
||||
|
||||
透明代理原理
|
||||
https://www.kernel.org/doc/html/latest/networking/tproxy.html
|
||||
|
||||
golang 示例
|
||||
https://github.com/LiamHaworth/go-tproxy/blob/master/tproxy_tcp.go
|
||||
|
||||
c 语言 示例
|
||||
https://github.com/FarFetchd/simple_tproxy_example/blob/master/tproxy_captive_portal.c
|
||||
|
||||
|
||||
关键点在于
|
||||
|
||||
1. 要使用 syscall.IP_TRANSPARENT 监听
|
||||
|
||||
2. 监听到的 连接 的 localAddr实际上是 真实的目标地址, 而不是我们监听的地址;
|
||||
|
||||
|
||||
我们在本包里要做的事情就是 模仿 上面的 golang示例,
|
||||
|
||||
但是,上面的go示例有一个特点, 它是直接利用客户端自己的地址+reuse端口的方法去拨号实际地址的,而我们不需要那样做。
|
||||
|
||||
而且, udp 的过程更加特殊。
|
||||
|
||||
总之,这种情况完全不适配 proxy.Server 的接口, 应该单独拿出来, 属于网络层的特殊情况.
|
||||
|
||||
另外就是,偶然发现,trojan-go也是使用的 上面的示例的代码。
|
||||
|
||||
同时,trojan-go还使用了
|
||||
https://github.com/cybozu-go/transocks/blob/master/original_dst_linux.go
|
||||
|
||||
Iptables
|
||||
|
||||
iptables配置教程:
|
||||
https://toutyrater.github.io/app/tproxy.html
|
||||
|
||||
下面把该教程的重要部分搬过来。
|
||||
|
||||
|
||||
ip rule add fwmark 1 table 100
|
||||
ip route add local 0.0.0.0/0 dev lo table 100
|
||||
|
||||
iptables -t mangle -N V2RAY
|
||||
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN
|
||||
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
|
||||
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1
|
||||
iptables -t mangle -A PREROUTING -j V2RAY
|
||||
|
||||
iptables -t mangle -N V2RAY_MASK
|
||||
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark --mark 0xff
|
||||
iptables -t mangle -A V2RAY_MASK -p udp -j MARK --set-mark 1
|
||||
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK --set-mark 1
|
||||
iptables -t mangle -A OUTPUT -j V2RAY_MASK
|
||||
|
||||
|
||||
Persistant iptables
|
||||
|
||||
单独设置iptables,重启后会消失. 下面是持久化方法
|
||||
|
||||
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4
|
||||
|
||||
vi /etc/systemd/system/tproxyrule.service
|
||||
|
||||
[Unit]
|
||||
Description=Tproxy rule
|
||||
After=network.target
|
||||
Wants=network.target
|
||||
|
||||
[Service]
|
||||
|
||||
Type=oneshot
|
||||
ExecStart=/sbin/ip rule add fwmark 1 table 100 ; /sbin/ip route add local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables-restore /etc/iptables/rules.v4
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
|
||||
systemctl enable tproxyrule
|
||||
|
||||
*/
|
||||
package tproxy
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,97 +1,3 @@
|
||||
/*
|
||||
Package tproxy implements tproxy.
|
||||
|
||||
透明代理只能用于linux。
|
||||
|
||||
About TProxy 关于透明代理
|
||||
|
||||
透明代理原理
|
||||
https://www.kernel.org/doc/html/latest/networking/tproxy.html
|
||||
|
||||
golang 示例
|
||||
https://github.com/LiamHaworth/go-tproxy/blob/master/tproxy_tcp.go
|
||||
|
||||
c 语言 示例
|
||||
https://github.com/FarFetchd/simple_tproxy_example/blob/master/tproxy_captive_portal.c
|
||||
|
||||
|
||||
关键点在于
|
||||
|
||||
1. 要使用 syscall.IP_TRANSPARENT 监听
|
||||
|
||||
2. 监听到的 连接 的 localAddr实际上是 真实的目标地址, 而不是我们监听的地址;
|
||||
|
||||
|
||||
我们在本包里要做的事情就是 模仿 上面的 golang示例,
|
||||
|
||||
但是,上面的go示例有一个特点, 它是直接利用客户端自己的地址+reuse端口的方法去拨号实际地址的,而我们不需要那样做。
|
||||
|
||||
而且, udp 的过程更加特殊。
|
||||
|
||||
总之,这种情况完全不适配 proxy.Server 的接口, 应该单独拿出来, 属于网络层的特殊情况.
|
||||
|
||||
另外就是,偶然发现,trojan-go也是使用的 上面的示例的代码。
|
||||
|
||||
同时,trojan-go还使用了
|
||||
https://github.com/cybozu-go/transocks/blob/master/original_dst_linux.go
|
||||
|
||||
Iptables
|
||||
|
||||
iptables配置教程:
|
||||
https://toutyrater.github.io/app/tproxy.html
|
||||
|
||||
下面把该教程的重要部分搬过来。
|
||||
|
||||
|
||||
ip rule add fwmark 1 table 100
|
||||
ip route add local 0.0.0.0/0 dev lo table 100
|
||||
|
||||
iptables -t mangle -N V2RAY
|
||||
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN
|
||||
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN
|
||||
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
|
||||
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1
|
||||
iptables -t mangle -A PREROUTING -j V2RAY
|
||||
|
||||
iptables -t mangle -N V2RAY_MASK
|
||||
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN
|
||||
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark --mark 0xff
|
||||
iptables -t mangle -A V2RAY_MASK -p udp -j MARK --set-mark 1
|
||||
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK --set-mark 1
|
||||
iptables -t mangle -A OUTPUT -j V2RAY_MASK
|
||||
|
||||
|
||||
Persistant iptables
|
||||
|
||||
单独设置iptables,重启后会消失. 下面是持久化方法
|
||||
|
||||
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4
|
||||
|
||||
vi /etc/systemd/system/tproxyrule.service
|
||||
|
||||
[Unit]
|
||||
Description=Tproxy rule
|
||||
After=network.target
|
||||
Wants=network.target
|
||||
|
||||
[Service]
|
||||
|
||||
Type=oneshot
|
||||
ExecStart=/sbin/ip rule add fwmark 1 table 100 ; /sbin/ip route add local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables-restore /etc/iptables/rules.v4
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
|
||||
systemctl enable tproxyrule
|
||||
|
||||
*/
|
||||
package tproxy
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user