Ejemplo n.º 1
0
func TryConnect(h string, networkSecret *secure.NetworkSecret, ln *LocalNode) (*RemoteNode, error) {
	host, portStr, errSplit := net.SplitHostPort(h)
	if errSplit != nil {
		return nil, errSplit
	}

	port, errConvert := strconv.Atoi(portStr)
	if errConvert != nil {
		return nil, errConvert
	}

	rn := new(RemoteNode)
	rn.lastHeartbeat = time.Now()
	rn.publicAddress = fmt.Sprintf("%s:%d", host, port+1)

	rn.logger = log.L(fmt.Sprintf("public %s", rn.publicAddress))
	rn.logger.Debug("trying to connect...")

	s, errSocket := utp.NewSocket("udp4", ":0")
	if errSocket != nil {
		rn.logger.Error("unable to crete a socket, %v", errSocket)
		return nil, errSocket
	}

	conn, errDial := s.DialTimeout(rn.publicAddress, 10*time.Second)
	if errDial != nil {
		rn.logger.Error("unable to dial, %s", errDial)
		return nil, errDial
	}

	rn.conn = conn
	rn.sessionKey = RandomBytes(16)

	if err := protocol.WriteEncodeHandshake(rn.conn, rn.sessionKey, networkSecret); err != nil {
		return nil, err
	}
	if _, okError := protocol.ReadDecodeOk(rn.conn); okError != nil {
		return nil, okError
	}

	peerInfo, errPeerInfo := protocol.ReadDecodePeerInfo(rn.conn)
	if errPeerInfo != nil {
		return nil, errPeerInfo
	}

	rn.privateIP = peerInfo.PrivateIP()

	// create new logger
	log.RemoveLogger(rn.logger.Name())
	rn.logger = log.L(fmt.Sprintf(rnLoggerFormat, rn.privateIP.String()))

	if err := protocol.WriteEncodePeerInfo(rn.conn, ln.State().PrivateIP); err != nil {
		return nil, err
	}

	rn.logger.Info("connected, with public address %q", rn.publicAddress)
	return rn, nil
}
Ejemplo n.º 2
0
func NewLocalNode(cfg *Config) (*LocalNode, error) {
	var err error
	n := new(LocalNode)
	n.logger = log.L("local")

	n.secret, err = secure.NetworkSecretUnmarshal(cfg.SecretKey)
	if err != nil {
		return nil, err
	}

	n.config = cfg
	n.config.NetworkID = n.secret.InfoHash()
	n.state = NewState(n.secret)

	n.services = make(map[string]Service)

	n.AddService(&NetTable{})
	n.AddService(&ListenerService{})
	n.AddService(&DiscoveryDHT{})
	n.AddService(&InterfaceService{})
	//n.AddService(&STUNService{})
	n.AddService(&UPnPService{})
	n.AddService(&HttpService{})
	return n, nil
}
Ejemplo n.º 3
0
func (nt *NetTable) Init(ln *LocalNode) error {
	nt.logger = log.L(nt.Name())
	nt.localNode = ln
	nt.dhtInChan = make(chan string, 10)
	nt.blackList = make(map[string]time.Time)
	nt.peers = make(map[string]*RemoteNode)
	return nil
}
Ejemplo n.º 4
0
func NewRemoteNode(conn net.Conn, sessionKey []byte, privateIP net.IP) *RemoteNode {
	return &RemoteNode{
		conn:          conn,
		sessionKey:    sessionKey,
		privateIP:     privateIP,
		publicAddress: conn.RemoteAddr().String(),
		logger:        log.L(fmt.Sprintf(rnLoggerFormat, privateIP.String())),
		lastHeartbeat: time.Now(),
	}
}
Ejemplo n.º 5
0
func (l *ListenerService) Init(ln *LocalNode) error {
	l.logger = log.L(l.Name())

	port := ln.State().ListenPort + 1
	l.logger.Info("listening on %d port", port)
	socket, err := utp.NewSocket("udp4", fmt.Sprintf("0.0.0.0:%d", port))
	if err != nil {
		return err
	}

	l.localNode = ln
	l.socket = socket
	return nil
}
Ejemplo n.º 6
0
func (is *InterfaceService) Init(ln *LocalNode) (err error) {
	is.logger = log.L(is.Name())
	is.ln = ln
	is.netTable = ln.NetTable()
	netSize, _ := ln.State().Secret.Net.Mask.Size()
	IPAddress := fmt.Sprintf("%s/%s", ln.State().PrivateIP, strconv.Itoa(netSize))

	if is.instance, err = network.CreateTunInterfaceWithIp("", IPAddress); err != nil {
		return err
	}

	if err = network.SetMTU(is.instance.Name(), 1400); err != nil {
		is.logger.Warning("unable to set mtu, %v", err)
	}
	return nil
}
Ejemplo n.º 7
0
const (
	TypeHandshake uint8 = iota
	TypeOk
	TypeHeartbeat
	TypeTransfer
	TypePeerInfo
)

const (
	CurrentVersion = 1
	bodyVectorLen  = 16
)

var (
	logger = log.L("proto")

	ErrorUnableToReadVector  = errors.New("unable to read vector")
	ErrorUnableToReadMessage = errors.New("unable to read message")
	ErrorUnknownType         = errors.New("unknown type")

	knownTypes = []uint8{
		TypeHandshake,
		TypeOk,
		TypeHeartbeat,
		TypeTransfer,
		TypePeerInfo,
	}
)

type (
Ejemplo n.º 8
0
func (s *UPnPService) Init(ln *LocalNode) error {
	s.logger = log.L(s.Name())
	s.mapping = new(upnp.Upnp)
	s.port = ln.State().ListenPort + 1
	return nil
}
Ejemplo n.º 9
0
func (s *STUNService) Init(ln *LocalNode) error {
	s.logger = log.L(s.Name())
	s.client = stun.NewClient()
	s.client.SetServerAddr(STUNAddress)
	return nil
}
Ejemplo n.º 10
0
func (d *DiscoveryDHT) Init(ln *LocalNode) error {
	d.logger = log.L(d.Name())
	d.localNode = ln
	d.stopChan = make(chan bool)
	return nil
}