Ejemplo n.º 1
0
func main() {
	flag.Parse()
	id, err := govpn.IDDecode(*IDRaw)
	if err != nil {
		log.Fatalln(err)
	}
	pub, _ := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
	if *verifierPath == "" {
		fmt.Println(hex.EncodeToString(pub[:]))
	} else {
		verifier, err := hex.DecodeString(govpn.StringFromFile(*verifierPath))
		if err != nil {
			log.Fatalln("Can not decode verifier:", err)
		}
		fmt.Println(subtle.ConstantTimeCompare(verifier[:], pub[:]) == 1)
	}
}
Ejemplo n.º 2
0
func confRead() map[govpn.PeerId]*govpn.PeerConf {
	data, err := ioutil.ReadFile(*confPath)
	if err != nil {
		log.Fatalln("Unable to read configuration:", err)
	}
	confsRaw := new(map[string]govpn.PeerConf)
	err = json.Unmarshal(data, confsRaw)
	if err != nil {
		log.Fatalln("Unable to parse configuration:", err)
	}

	confs := make(map[govpn.PeerId]*govpn.PeerConf, len(*confsRaw))
	for peerIdRaw, pc := range *confsRaw {
		peerId, err := govpn.IDDecode(peerIdRaw)
		if err != nil {
			log.Fatalln("Invalid peer ID:", peerIdRaw, err)
		}
		conf := govpn.PeerConf{
			Id:    peerId,
			Name:  pc.Name,
			Up:    pc.Up,
			Down:  pc.Down,
			Noise: pc.Noise,
			CPR:   pc.CPR,
		}
		if pc.TimeoutInt <= 0 {
			pc.TimeoutInt = govpn.TimeoutDefault
		}
		conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)

		if len(pc.Verifier) != ed25519.PublicKeySize*2 {
			log.Fatalln("Verifier must be 64 hex characters long")
		}
		keyDecoded, err := hex.DecodeString(string(pc.Verifier))
		if err != nil {
			log.Fatalln("Unable to decode the key:", err.Error(), pc.Verifier)
		}
		conf.DSAPub = new([ed25519.PublicKeySize]byte)
		copy(conf.DSAPub[:], keyDecoded)

		confs[*peerId] = &conf
	}
	return confs
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()
	timeout := *timeoutP
	var err error
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)

	govpn.MTU = *mtu

	id, err := govpn.IDDecode(*IDRaw)
	if err != nil {
		log.Fatalln(err)
	}

	if *egdPath != "" {
		log.Println("Using", *egdPath, "EGD")
		govpn.EGDInit(*egdPath)
	}

	pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
	conf := &govpn.PeerConf{
		Id:          id,
		Timeout:     time.Second * time.Duration(timeout),
		NoiseEnable: *noisy,
		CPR:         *cpr,
		DSAPub:      pub,
		DSAPriv:     priv,
	}
	govpn.PeersInitDummy(id, conf)

	var conn io.Writer
	var sink chan []byte
	var ready chan struct{}
	switch *proto {
	case "udp":
		conn, sink, ready = startUDP()
	case "tcp":
		if *proxyAddr != "" {
			conn, sink, ready = proxyTCP()
		} else {
			conn, sink, ready = startTCP()
		}
	default:
		log.Fatalln("Unknown protocol specified")
	}

	tap, ethSink, ethReady, _, err := govpn.TAPListen(
		*ifaceName,
		time.Second*time.Duration(timeout),
		*cpr,
	)
	if err != nil {
		log.Fatalln("Can not listen on TAP interface:", err)
	}

	timeouts := 0
	firstUpCall := true
	var peer *govpn.Peer
	var ethPkt []byte
	var pkt []byte
	knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})

	log.Println(govpn.VersionGet())
	log.Println("Connected to", *proto, *remoteAddr)
	log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
	if *stats != "" {
		log.Println("Stats are going to listen on", *stats)
		statsPort, err := net.Listen("tcp", *stats)
		if err != nil {
			log.Fatalln("Can not listen on stats port:", err)
		}
		go govpn.StatsProcessor(statsPort, &knownPeers)
	}

	termSignal := make(chan os.Signal, 1)
	signal.Notify(termSignal, os.Interrupt, os.Kill)

	log.Println("Starting handshake")
	handshake := govpn.HandshakeStart(*remoteAddr, conn, conf)

MainCycle:
	for {
		if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
			peer.Zero()
			peer = nil
			handshake = govpn.HandshakeStart(*remoteAddr, conn, conf)
			log.Println("Rehandshaking")
		}
		select {
		case <-termSignal:
			break MainCycle
		case ethPkt = <-ethSink:
			if peer == nil {
				if len(ethPkt) > 0 {
					ethReady <- struct{}{}
				}
				continue
			}
			peer.EthProcess(ethPkt, ethReady)
		case pkt = <-sink:
			timeouts++
			if timeouts >= timeout {
				break MainCycle
			}
			if pkt == nil {
				ready <- struct{}{}
				continue
			}

			if peer == nil {
				if govpn.IDsCache.Find(pkt) == nil {
					log.Println("Invalid identity in handshake packet")
					ready <- struct{}{}
					continue
				}
				if p := handshake.Client(pkt); p != nil {
					log.Println("Handshake completed")
					if firstUpCall {
						go govpn.ScriptCall(*upPath, *ifaceName)
						firstUpCall = false
					}
					peer = p
					handshake.Zero()
					handshake = nil
				}
				ready <- struct{}{}
				continue
			}
			if peer == nil {
				ready <- struct{}{}
				continue
			}
			if peer.PktProcess(pkt, tap, ready) {
				timeouts = 0
			}
		}
	}
	govpn.ScriptCall(*downPath, *ifaceName)
}
Ejemplo n.º 4
0
func main() {
	flag.Parse()
	timeout = *timeoutP
	var err error
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)

	govpn.MTU = *mtu

	id, err := govpn.IDDecode(*IDRaw)
	if err != nil {
		log.Fatalln(err)
	}

	if *egdPath != "" {
		log.Println("Using", *egdPath, "EGD")
		govpn.EGDInit(*egdPath)
	}

	pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
	conf = &govpn.PeerConf{
		Id:      id,
		Timeout: time.Second * time.Duration(timeout),
		Noise:   *noisy,
		CPR:     *cpr,
		DSAPub:  pub,
		DSAPriv: priv,
	}
	idsCache = govpn.NewCipherCache([]govpn.PeerId{*id})
	log.Println(govpn.VersionGet())

	tap, err = govpn.TAPListen(*ifaceName)
	if err != nil {
		log.Fatalln("Can not listen on TAP interface:", err)
	}

	log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
	if *stats != "" {
		log.Println("Stats are going to listen on", *stats)
		statsPort, err := net.Listen("tcp", *stats)
		if err != nil {
			log.Fatalln("Can not listen on stats port:", err)
		}
		go govpn.StatsProcessor(statsPort, &knownPeers)
	}

	termSignal := make(chan os.Signal, 1)
	signal.Notify(termSignal, os.Interrupt, os.Kill)

MainCycle:
	for {
		timeouted := make(chan struct{})
		rehandshaking := make(chan struct{})
		termination := make(chan struct{})
		switch *proto {
		case "udp":
			go startUDP(timeouted, rehandshaking, termination)
		case "tcp":
			if *proxyAddr != "" {
				go proxyTCP(timeouted, rehandshaking, termination)
			} else {
				go startTCP(timeouted, rehandshaking, termination)
			}
		default:
			log.Fatalln("Unknown protocol specified")
		}
		select {
		case <-termSignal:
			log.Fatalln("Finishing")
			termination <- struct{}{}
			break MainCycle
		case <-timeouted:
			break MainCycle
		case <-rehandshaking:
		}
		close(timeouted)
		close(rehandshaking)
		close(termination)
	}
	govpn.ScriptCall(*downPath, *ifaceName)
}