forked from Jigsaw-Code/outline-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Alim Shapiev
committed
Nov 2, 2024
1 parent
6da1412
commit bf61e14
Showing
7 changed files
with
153 additions
and
78 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
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
package signature | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/sys/unix" | ||
"net" | ||
"unsafe" | ||
) | ||
|
||
const socketFlag = 14 | ||
|
||
type signature struct { | ||
Addr [16]byte | ||
Len uint16 | ||
Flags uint16 | ||
Key [80]byte | ||
} | ||
|
||
func Add(conn *net.TCPConn, remoteAddr string, data string) error { | ||
ip := net.ParseIP(remoteAddr) | ||
if ip == nil { | ||
return fmt.Errorf("invalid remote IP address: %s", remoteAddr) | ||
} | ||
|
||
address, err := ip.To16().MarshalText() | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal IP address: %w", err) | ||
} | ||
|
||
key := []byte(data) | ||
|
||
sig := signature{ | ||
Addr: [16]byte(address), | ||
Len: uint16(len(data)), | ||
Key: [80]byte(key), | ||
} | ||
|
||
if err := setOption(conn, sig); err != nil { | ||
return fmt.Errorf("failed to set socket option: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setOption(conn *net.TCPConn, md5sig signature) error { | ||
file, err := conn.File() | ||
if err != nil { | ||
return fmt.Errorf("failed to get file descriptor: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
size := unsafe.Sizeof(md5sig) | ||
buffer := (*[unsafe.Sizeof(md5sig)]byte)(unsafe.Pointer(&md5sig))[:size] | ||
fd := int(file.Fd()) | ||
|
||
err = unix.SetsockoptString(fd, unix.IPPROTO_TCP, socketFlag, string(buffer)) | ||
if err != nil { | ||
return fmt.Errorf("failed to set TCP_MD5SIG: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Remove(conn *net.TCPConn) error { | ||
file, err := conn.File() | ||
if err != nil { | ||
return fmt.Errorf("failed to get underlying file descriptor: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
fd := int(file.Fd()) | ||
|
||
err = unix.SetsockoptString(fd, unix.IPPROTO_TCP, socketFlag, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to clear TCP_MD5SIG: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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
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,33 @@ | ||
package ttl | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/net/ipv4" | ||
"golang.org/x/net/ipv6" | ||
"net" | ||
"net/netip" | ||
) | ||
|
||
func Set(conn net.Conn, ttl int) (old int, err error) { | ||
addr, err := netip.ParseAddrPort(conn.RemoteAddr().String()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
switch { | ||
case addr.Addr().Is4(): | ||
conn := ipv4.NewConn(conn) | ||
old, _ = conn.TTL() | ||
if err := conn.SetTTL(ttl); err != nil { | ||
return 0, fmt.Errorf("failed to set TTL: %w", err) | ||
} | ||
case addr.Addr().Is6(): | ||
conn := ipv6.NewConn(conn) | ||
old, _ = conn.HopLimit() | ||
if err := conn.SetHopLimit(ttl); err != nil { | ||
return 0, fmt.Errorf("failed to set hop limit: %w", err) | ||
} | ||
} | ||
|
||
return | ||
} |