X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=common.go;h=f9c5d1a44e04d8435b7f2c7a2335d4f29a74cf78;hb=refs%2Fheads%2Fdevelop;hp=b42673c98925f32be3b63948f7eaf30eccb186e9;hpb=96f3f25cbb28e0c6d3ac777885d8336a815710ce;p=govpn.git diff --git a/common.go b/common.go index b42673c..f9c5d1a 100644 --- a/common.go +++ b/common.go @@ -1,11 +1,10 @@ /* GoVPN -- simple secure free software virtual private network daemon -Copyright (C) 2014-2015 Sergey Matveev +Copyright (C) 2014-2020 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,58 +18,54 @@ along with this program. If not, see . package govpn import ( - "bytes" - "encoding/hex" - "io/ioutil" "log" "os" "os/exec" + "runtime" +) + +const ( + TimeoutDefault = 60 + EtherSize = 14 + MTUMax = 9000 + EtherSize + 1 + MTUDefault = 1500 + EtherSize + 1 + + ENV_IFACE = "GOVPN_IFACE" + ENV_REMOTE = "GOVPN_REMOTE" ) var ( - MTU int - Timeout int - Noncediff int - Version string + Version string = "UNKNOWN" ) // Call external program/script. // You have to specify path to it and (inteface name as a rule) something // that will be the first argument when calling it. Function will return // it's output and possible error. -func ScriptCall(path, ifaceName string) ([]byte, error) { +func ScriptCall(path, ifaceName, remoteAddr string) ([]byte, error) { if path == "" { return nil, nil } if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { return nil, err } - cmd := exec.Command(path, ifaceName) - var out bytes.Buffer - cmd.Stdout = &out - err := cmd.Run() - result := out.Bytes() + cmd := exec.Command(path) + cmd.Env = append(cmd.Env, ENV_IFACE+"="+ifaceName) + cmd.Env = append(cmd.Env, ENV_REMOTE+"="+remoteAddr) + out, err := cmd.CombinedOutput() if err != nil { - log.Println("Script error", path, err, string(result)) + log.Println("Script error", path, err, string(out)) } - return result, err + return out, err } -// Read authentication key from the file. -// Key is 64 hexadecimal chars long. -func KeyRead(path string) *[KeySize]byte { - keyData, err := ioutil.ReadFile(path) - if err != nil { - panic("Unable to read keyfile: " + err.Error()) - } - if len(keyData) < 64 { - panic("Key must be 64 hex characters long") +// Zero each byte. +func SliceZero(data []byte) { + for i := 0; i < len(data); i++ { + data[i] = 0 } - keyDecoded, err := hex.DecodeString(string(keyData[0:64])) - if err != nil { - panic("Unable to decode the key: " + err.Error()) - } - key := new([KeySize]byte) - copy(key[:], keyDecoded) - return key +} + +func VersionGet() string { + return "GoVPN version " + Version + " built with " + runtime.Version() }