Пример #1
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)
}
Пример #2
0
func main() {
	flag.Parse()
	timeout = *timeoutP
	var err error
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)

	govpn.MTU = *mtu

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

	verifier, err := govpn.VerifierFromString(*verifierRaw)
	if err != nil {
		log.Fatalln(err)
	}
	priv := verifier.PasswordApply(govpn.StringFromFile(*keyPath))
	conf = &govpn.PeerConf{
		Id:       verifier.Id,
		Timeout:  time.Second * time.Duration(timeout),
		Noise:    *noisy,
		CPR:      *cpr,
		Verifier: verifier,
		DSAPriv:  priv,
	}
	idsCache = govpn.NewCipherCache([]govpn.PeerId{*verifier.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{})
		if *proxyAddr != "" {
			*proto = "tcp"
		}
		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)
}
Пример #3
0
func main() {
	flag.Parse()
	timeout := time.Second * time.Duration(govpn.TimeoutDefault)
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
	log.Println(govpn.VersionGet())

	govpn.MTU = *mtu
	govpn.PeersInit(*peersPath)

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

	sink := make(chan Pkt)
	switch *proto {
	case "udp":
		startUDP(sink)
	case "tcp":
		startTCP(sink)
	case "all":
		startUDP(sink)
		startTCP(sink)
	default:
		log.Fatalln("Unknown protocol specified")
	}

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

	hsHeartbeat := time.Tick(timeout)
	go func() { <-hsHeartbeat }()

	var state *govpn.Handshake
	var peerState *PeerState
	var peer *govpn.Peer
	var exists bool
	states := make(map[string]*govpn.Handshake)
	peers := make(map[string]*PeerState)
	peerReadySink := make(chan PeerReadyEvent)
	knownPeers := govpn.KnownPeers(make(map[string]**govpn.Peer))
	var peerReady PeerReadyEvent
	var pkt Pkt
	var ethEvent EthEvent
	var peerId *govpn.PeerId
	var peerConf *govpn.PeerConf
	var handshakeProcessForce bool
	ethSink := make(chan EthEvent)

	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)
	}
	if *proxy != "" {
		go proxyStart(sink)
	}
	log.Println("Server started")

MainCycle:
	for {
		select {
		case <-termSignal:
			break MainCycle
		case <-hsHeartbeat:
			now := time.Now()
			for addr, hs := range states {
				if hs.LastPing.Add(timeout).Before(now) {
					log.Println("Deleting handshake state", addr)
					hs.Zero()
					delete(states, addr)
				}
			}
			for addr, state := range peers {
				if state.peer.LastPing.Add(timeout).Before(now) {
					log.Println("Deleting peer", state.peer)
					delete(peers, addr)
					delete(knownPeers, addr)
					downPath := path.Join(
						govpn.PeersPath,
						state.peer.Id.String(),
						"down.sh",
					)
					go govpn.ScriptCall(downPath, state.tap.Name)
					state.terminate <- struct{}{}
					state.peer.Zero()
				}
			}
		case peerReady = <-peerReadySink:
			for addr, state := range peers {
				if state.tap.Name != peerReady.iface {
					continue
				}
				delete(peers, addr)
				delete(knownPeers, addr)
				state.terminate <- struct{}{}
				state.peer.Zero()
				break
			}
			state := NewPeerState(peerReady.peer, peerReady.iface)
			if state == nil {
				continue
			}
			peers[peerReady.peer.Addr] = state
			knownPeers[peerReady.peer.Addr] = &peerReady.peer
			states[peerReady.peer.Addr].Zero()
			delete(states, peerReady.peer.Addr)
			log.Println("Registered interface", peerReady.iface, "with peer", peer)
			go func(state *PeerState) {
				for data := range state.sink {
					ethSink <- EthEvent{
						peer:  state.peer,
						data:  data,
						ready: state.ready,
					}
				}
			}(state)
		case ethEvent = <-ethSink:
			if s, exists := peers[ethEvent.peer.Addr]; !exists || s.peer != ethEvent.peer {
				continue
			}
			ethEvent.peer.EthProcess(ethEvent.data, ethEvent.ready)
		case pkt = <-sink:
			if pkt.data == nil {
				pkt.ready <- struct{}{}
				continue
			}
			handshakeProcessForce = false
		HandshakeProcess:
			if _, exists = peers[pkt.addr]; handshakeProcessForce || !exists {
				peerId = govpn.IDsCache.Find(pkt.data)
				if peerId == nil {
					log.Println("Unknown identity from", pkt.addr)
					pkt.ready <- struct{}{}
					continue
				}
				peerConf = peerId.Conf()
				if peerConf == nil {
					log.Println("Can not get peer configuration", peerId.String())
					pkt.ready <- struct{}{}
					continue
				}
				state, exists = states[pkt.addr]
				if !exists {
					state = govpn.HandshakeNew(pkt.addr, pkt.conn, peerConf)
					states[pkt.addr] = state
				}
				peer = state.Server(pkt.data)
				if peer != nil {
					log.Println("Peer handshake finished", peer)
					if _, exists = peers[pkt.addr]; exists {
						go func() {
							peerReadySink <- PeerReadyEvent{
								peer, peers[pkt.addr].tap.Name,
							}
						}()
					} else {
						go func() {
							upPath := path.Join(govpn.PeersPath, peer.Id.String(), "up.sh")
							result, err := govpn.ScriptCall(upPath, "")
							if err != nil {
								return
							}
							sepIndex := bytes.Index(result, []byte{'\n'})
							if sepIndex < 0 {
								sepIndex = len(result)
							}
							ifaceName := string(result[:sepIndex])
							peerReadySink <- PeerReadyEvent{peer, ifaceName}
						}()
					}
				}
				if !handshakeProcessForce {
					pkt.ready <- struct{}{}
				}
				continue
			}
			peerState, exists = peers[pkt.addr]
			if !exists {
				pkt.ready <- struct{}{}
				continue
			}
			// If it fails during processing, then try to work with it
			// as with handshake packet
			if !peerState.peer.PktProcess(pkt.data, peerState.tap, pkt.ready) {
				handshakeProcessForce = true
				goto HandshakeProcess
			}
		}
	}
}
Пример #4
0
func main() {
	flag.Parse()
	timeout := time.Second * time.Duration(govpn.TimeoutDefault)
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
	log.Println(govpn.VersionGet())

	govpn.MTU = *mtu
	confInit()
	knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))

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

	switch *proto {
	case "udp":
		startUDP()
	case "tcp":
		startTCP()
	case "all":
		startUDP()
		startTCP()
	default:
		log.Fatalln("Unknown protocol specified")
	}

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

	hsHeartbeat := time.Tick(timeout)
	go func() { <-hsHeartbeat }()

	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)
	}
	if *proxy != "" {
		go proxyStart()
	}
	log.Println("Server started")

	var needsDeletion bool
MainCycle:
	for {
		select {
		case <-termSignal:
			break MainCycle
		case <-hsHeartbeat:
			now := time.Now()
			hsLock.Lock()
			for addr, hs := range handshakes {
				if hs.LastPing.Add(timeout).Before(now) {
					log.Println("Deleting handshake state", addr)
					hs.Zero()
					delete(handshakes, addr)
				}
			}
			peersLock.Lock()
			peersByIdLock.Lock()
			kpLock.Lock()
			for addr, ps := range peers {
				ps.peer.BusyR.Lock()
				needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
				ps.peer.BusyR.Unlock()
				if needsDeletion {
					log.Println("Deleting peer", ps.peer)
					delete(peers, addr)
					delete(knownPeers, addr)
					delete(peersById, *ps.peer.Id)
					go govpn.ScriptCall(
						confs[*ps.peer.Id].Down,
						ps.tap.Name,
					)
					ps.terminator <- struct{}{}
				}
			}
			hsLock.Unlock()
			peersLock.Unlock()
			peersByIdLock.Unlock()
			kpLock.Unlock()
		}
	}
}