-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow firewall marking packets on Linux (#436)
- Loading branch information
Showing
11 changed files
with
115 additions
and
2 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 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
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,52 @@ | ||
//go:build linux | ||
|
||
package clients | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"log" | ||
"net" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const fwmarkIoctl int = 36 /* unix.SO_MARK */ | ||
|
||
var ErrUnknownConnType = errors.New("not a known conn type") | ||
|
||
func setFirewallMark(conn net.Conn, mark int) error { | ||
var err error | ||
var syscallConn syscall.Conn | ||
|
||
switch typedConn := conn.(type) { | ||
case syscall.Conn: | ||
syscallConn = typedConn | ||
case *tls.Conn: | ||
return setFirewallMark(typedConn.NetConn(), mark) | ||
default: | ||
log.Printf("Unknown conn type: %T (%v)", typedConn, typedConn) | ||
err = ErrUnknownConnType | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
var operr error | ||
fd, err := syscallConn.SyscallConn() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = fd.Control(func(fd uintptr) { | ||
operr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, fwmarkIoctl, int(mark)) | ||
}) | ||
|
||
if err == nil { | ||
return operr | ||
} | ||
|
||
return err | ||
} |
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,11 @@ | ||
//go:build !linux | ||
|
||
package clients | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
func setFirewallMark(conn net.Conn, mark int) error { | ||
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