Skip to content

Commit

Permalink
feat[bittorrent]: listen port from 30000 to 31000 (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn-Huang-Tron authored Nov 25, 2022
1 parent ae7715e commit 17250a9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/commands/bittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ var downloadBTCmd = &cmds.Command{
return nil
},
}

var minBTListenPort = 30000
var maxBTListenPort = 31000

var serveBTCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Serve as a bittorrent client with the specified files.",
Expand All @@ -321,13 +325,24 @@ var serveBTCmd = &cmds.Command{
return fmt.Errorf("you must provide the paths of some files that you want to serve as seeds")
}
cfg := torrent.NewDefaultClientConfig()
cfg.ListenPort = 0
cfg.ListenPort = minBTListenPort
cfg.Seed = true
retry:
cl, err := torrent.NewClient(cfg)
if err != nil {
return fmt.Errorf("new torrent client: %w", err)
if strings.Contains(err.Error(), "address already in use") {
fmt.Println(err)
cfg.ListenPort = cfg.ListenPort + 1
if cfg.ListenPort > maxBTListenPort {
return fmt.Errorf("we have try all the port between %d and %d ,but they are all in used", minBTListenPort, maxBTListenPort)
}
goto retry
} else {
return fmt.Errorf("new torrent client: %w", err)
}
}
defer cl.Close()

for _, filePath := range filePaths {
totalLength, err := totalLength(filePath)
if err != nil {
Expand Down

0 comments on commit 17250a9

Please sign in to comment.