implemented child process
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jake Hillion 2021-03-26 18:13:39 +00:00
parent 2376ee24fd
commit e33c1742e9
3 changed files with 37 additions and 4 deletions

View File

@ -40,7 +40,7 @@ type Configuration struct {
type Host struct {
Crypto string `validate:"required,oneof=None Blake2s"`
SharedKey string `validate:"required_if=Crypto Blake2s"`
MTU uint
MTU uint `validate:"required,min=576"`
}
type Peer struct {

33
main.go
View File

@ -28,6 +28,7 @@ func main() {
// 2) create a tun adapter
// 3) spawn a child
// 4) exit
o, err := flags.ParseFlags()
if err != nil {
if errors.Is(err, flags.PrintedHelpErr) {
@ -56,8 +57,40 @@ func main() {
if err := os.Setenv(ENV_NC_CONFIG_PATH, o.ConfigFile); err != nil {
panic(err)
}
log.Println("switching to foreground")
goto FOREGROUND
}
files := make([]*os.File, 4)
files[0], _ = os.Open(os.DevNull) // stdin
files[1], _ = os.Open(os.DevNull) // stderr
files[2], _ = os.Open(os.DevNull) // stdout
files[3] = t.File()
env := os.Environ()
env = append(env, fmt.Sprintf("%s=3", ENV_NC_TUN_FD))
env = append(env, fmt.Sprintf("%s=%s", ENV_NC_CONFIG_PATH, o.ConfigFile))
attr := os.ProcAttr{
Env: env,
Files: files,
}
path, err := os.Executable()
if err != nil {
panic(err)
}
process, err := os.StartProcess(
path,
os.Args,
&attr,
)
if err != nil {
panic(err)
}
_ = process.Release()
return
}

View File

@ -51,7 +51,7 @@ func NewFromFile(fd uintptr, mtu int) (ss *SourceSink, err error) {
if strings.Contains(iface.Flags.String(), "up") {
log.Println("wgtun is up")
log.Println("tun is up")
ss.up = true
return
}
@ -77,7 +77,7 @@ func (t *SourceSink) Source() (proxy.Packet, error) {
buf := make([]byte, t.mtu)
read, err := t.tun.Read(buf, t.mtu)
read, err := t.tun.Read(buf, 4)
if err != nil {
return nil, err
}
@ -97,7 +97,7 @@ func (t *SourceSink) Sink(packet proxy.Packet) error {
t.upMu.Unlock()
}
_, err := t.tun.Write(packet.Contents(), t.mtu)
_, err := t.tun.Write(packet.Contents(), 4)
if err != nil {
switch err.(type) {
case *os.PathError: