예제 #1
0
파일: main.go 프로젝트: gbjk/torrent
func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Parse()
	switch len(*infoHash) {
	case 20:
	case 40:
		_, err := fmt.Sscanf(*infoHash, "%x", infoHash)
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal("require 20 byte infohash")
	}
	var err error
	s, err = dht.NewServer(&dht.ServerConfig{
		Addr: *serveAddr,
	})
	if err != nil {
		log.Fatal(err)
	}
	err = loadTable()
	if err != nil {
		log.Fatalf("error loading table: %s", err)
	}
	log.Printf("dht server on %s, ID is %x", s.Addr(), s.ID())
	setupSignals()
}
예제 #2
0
파일: main.go 프로젝트: soul9/torrent
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	timeout := flag.Duration("timeout", -1, "maximum timeout")
	flag.Parse()
	pingStrAddrs := flag.Args()
	if len(pingStrAddrs) == 0 {
		os.Stderr.WriteString("u must specify addrs of nodes to ping e.g. router.bittorrent.com:6881\n")
		os.Exit(2)
	}
	s, err := dht.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("dht server on %s", s.Addr())
	pingResponses := make(chan pingResponse)
	timeoutChan := make(chan struct{})
	go func() {
		for i, netloc := range pingStrAddrs {
			if i != 0 {
				time.Sleep(1 * time.Millisecond)
			}
			addr, err := net.ResolveUDPAddr("udp4", netloc)
			if err != nil {
				log.Fatal(err)
			}
			t, err := s.Ping(addr)
			if err != nil {
				log.Fatal(err)
			}
			start := time.Now()
			t.SetResponseHandler(func(addr string) func(dht.Msg) {
				return func(resp dht.Msg) {
					pingResponses <- pingResponse{
						addr: addr,
						krpc: resp,
						rtt:  time.Now().Sub(start),
					}
				}
			}(netloc))
		}
		if *timeout >= 0 {
			time.Sleep(*timeout)
			close(timeoutChan)
		}
	}()
	responses := 0
pingResponses:
	for _ = range pingStrAddrs {
		select {
		case resp := <-pingResponses:
			responses++
			fmt.Printf("%-65s %s\n", fmt.Sprintf("%x (%s):", resp.krpc.R.ID, resp.addr), resp.rtt)
		case <-timeoutChan:
			break pingResponses
		}
	}
	// timeouts := len(pingStrAddrs) - responses
	fmt.Printf("%d/%d responses (%f%%)\n", responses, len(pingStrAddrs), 100*float64(responses)/float64(len(pingStrAddrs)))
}
예제 #3
0
func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	flag.Parse()
	var err error
	s, err = dht.NewServer(&dht.ServerConfig{
		Addr: *serveAddr,
	})
	if err != nil {
		log.Fatal(err)
	}
	err = loadTable()
	if err != nil {
		log.Fatalf("error loading table: %s", err)
	}
	log.Printf("dht server on %s, ID is %q", s.Addr(), s.ID())
	setupSignals()
}
예제 #4
0
파일: main.go 프로젝트: jakop345/torrent
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	var args = struct {
		Timeout time.Duration
		Nodes   []string `type:"pos" arity:"+" help:"nodes to ping e.g. router.bittorrent.com:6881"`
	}{
		Timeout: math.MaxInt64,
	}
	tagflag.Parse(&args)
	s, err := dht.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("dht server on %s", s.Addr())
	timeout := time.After(args.Timeout)
	pongChan := make(chan pong)
	startPings(s, pongChan, args.Nodes)
	numResp := receivePongs(pongChan, timeout, len(args.Nodes))
	fmt.Printf("%d/%d responses (%f%%)\n", numResp, len(args.Nodes), 100*float64(numResp)/float64(len(args.Nodes)))
}
예제 #5
0
파일: client.go 프로젝트: jakop345/torrent
// Creates a new client.
func NewClient(cfg *Config) (cl *Client, err error) {
	if cfg == nil {
		cfg = &Config{}
	}

	defer func() {
		if err != nil {
			cl = nil
		}
	}()
	cl = &Client{
		halfOpenLimit:     socketsPerTorrent,
		config:            *cfg,
		defaultStorage:    cfg.DefaultStorage,
		dopplegangerAddrs: make(map[string]struct{}),
		torrents:          make(map[metainfo.Hash]*Torrent),
	}
	missinggo.CopyExact(&cl.extensionBytes, defaultExtensionBytes)
	cl.event.L = &cl.mu
	if cl.defaultStorage == nil {
		cl.defaultStorage = storage.NewFile(cfg.DataDir)
	}
	if cfg.IPBlocklist != nil {
		cl.ipBlockList = cfg.IPBlocklist
	}

	if cfg.PeerID != "" {
		missinggo.CopyExact(&cl.peerID, cfg.PeerID)
	} else {
		o := copy(cl.peerID[:], bep20)
		_, err = rand.Read(cl.peerID[o:])
		if err != nil {
			panic("error generating peer id")
		}
	}

	cl.tcpListener, cl.utpSock, cl.listenAddr, err = listen(
		!cl.config.DisableTCP,
		!cl.config.DisableUTP,
		func() string {
			if cl.config.DisableIPv6 {
				return "4"
			} else {
				return ""
			}
		}(),
		cl.config.ListenAddr)
	if err != nil {
		return
	}
	if cl.tcpListener != nil {
		go cl.acceptConnections(cl.tcpListener, false)
	}
	if cl.utpSock != nil {
		go cl.acceptConnections(cl.utpSock, true)
	}
	if !cfg.NoDHT {
		dhtCfg := cfg.DHTConfig
		if dhtCfg.IPBlocklist == nil {
			dhtCfg.IPBlocklist = cl.ipBlockList
		}
		dhtCfg.Addr = firstNonEmptyString(dhtCfg.Addr, cl.listenAddr, cl.config.ListenAddr)
		if dhtCfg.Conn == nil && cl.utpSock != nil {
			dhtCfg.Conn = cl.utpSock
		}
		cl.dHT, err = dht.NewServer(&dhtCfg)
		if err != nil {
			return
		}
	}

	return
}