This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
sockopt_linux.go
73 lines (60 loc) · 1.56 KB
/
sockopt_linux.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package netLayer
import (
"go.uber.org/zap"
"golang.org/x/sys/unix"
"syscall"
"github.com/e1732a364fed/v2ray_simple/utils"
)
func SetSockOpt(fd int, sockopt *Sockopt, isudp bool, isipv6 bool) {
if sockopt == nil {
return
}
if sockopt.Somark != 0 {
setSomark(fd, sockopt.Somark)
}
if sockopt.TProxy {
setTproxy(fd)
if isudp {
setTproxy_udp(fd, isipv6)
}
}
if sockopt.Device != "" {
bindToDevice(fd, sockopt.Device)
}
}
func bindToDevice(fd int, device string) {
if err := unix.BindToDevice(fd, device); err != nil {
if ce := utils.CanLogErr("BindToDevice failed"); ce != nil {
ce.Write(zap.Error(err))
}
}
}
func setTproxy(fd int) {
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
if ce := utils.CanLogErr("setTproxy failed"); ce != nil {
ce.Write(zap.Error(err))
}
}
}
func setTproxy_udp(fd int, isipv6 bool) {
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1); err != nil {
if ce := utils.CanLogErr("set IP_RECVORIGDSTADDR failed"); ce != nil {
ce.Write(zap.Error(err))
}
return
}
if isipv6 {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1); err != nil {
if ce := utils.CanLogErr("set IPV6_RECVORIGDSTADDR failed"); ce != nil {
ce.Write(zap.Error(err))
}
}
}
}
func setSomark(fd int, somark int) {
if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, somark); err != nil {
if ce := utils.CanLogErr("setSomark failed"); ce != nil {
ce.Write(zap.Error(err))
}
}
}