Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying incoming torrent port #27

Merged
merged 3 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,77 @@ type Client struct {
Torrent *torrent.Torrent
Progress int64
Uploaded int64
Port int
Seed bool
Config ClientConfig
}

// ClientConfig specifies the behaviour of a client.
type ClientConfig struct {
TorrentPath string
Port int
TorrentPort int
Seed bool
TCP bool
MaxConnections int
}

// NewClientConfig creates a new default configuration.
func NewClientConfig() ClientConfig {
return ClientConfig{
Port: 8080,
TorrentPort: 50007,
Seed: false,
TCP: true,
MaxConnections: 200,
}
}

// NewClient creates a new torrent client based on a magnet or a torrent file.
// If the torrent file is on http, we try downloading it.
func NewClient(torrentPath string, port int, seed bool, tcp bool, maxConnections int) (client Client, err error) {
func NewClient(cfg ClientConfig) (client Client, err error) {
var t *torrent.Torrent
var c *torrent.Client

client.Port = port
client.Config = cfg

// Create client.
c, err = torrent.NewClient(&torrent.Config{
DataDir: os.TempDir(),
NoUpload: !seed,
Seed: seed,
DisableTCP: !tcp,
NoUpload: !cfg.Seed,
Seed: cfg.Seed,
DisableTCP: !cfg.TCP,
ListenAddr: fmt.Sprintf(":%d", cfg.TorrentPort),
})

if err != nil {
return client, ClientError{Type: "creating torrent client", Origin: err}
}

client.Client = c
client.Seed = seed

// Add torrent.

// Add as magnet url.
if strings.HasPrefix(torrentPath, "magnet:") {
if t, err = c.AddMagnet(torrentPath); err != nil {
if strings.HasPrefix(cfg.TorrentPath, "magnet:") {
if t, err = c.AddMagnet(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "adding torrent", Origin: err}
}
} else {
// Otherwise add as a torrent file.

// If it's online, we try downloading the file.
if isHTTP.MatchString(torrentPath) {
if torrentPath, err = downloadFile(torrentPath); err != nil {
if isHTTP.MatchString(cfg.TorrentPath) {
if cfg.TorrentPath, err = downloadFile(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "downloading torrent file", Origin: err}
}
}

if t, err = c.AddTorrentFromFile(torrentPath); err != nil {
if t, err = c.AddTorrentFromFile(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "adding torrent to the client", Origin: err}
}
}

client.Torrent = t
client.Torrent.SetMaxEstablishedConns(maxConnections)
client.Torrent.SetMaxEstablishedConns(cfg.MaxConnections)

go func() {
<-t.GotInfo()
Expand Down Expand Up @@ -183,15 +203,15 @@ func (c *Client) Render() {
fmt.Println(t.Info().Name)
fmt.Println(strings.Repeat("=", len(t.Info().Name)))
if c.ReadyForPlayback() {
fmt.Printf("Stream: \thttp://localhost:%d\n", c.Port)
fmt.Printf("Stream: \thttp://localhost:%d\n", c.Config.Port)
}
if currentProgress > 0 {
fmt.Printf("Progress: \t%s / %s %.2f%%\n", complete, size, c.percentage())
}
if currentProgress < t.Info().TotalLength() {
fmt.Printf("Download speed: %s\n", downloadSpeed)
}
if c.Seed {
if c.Config.Seed {
fmt.Printf("Upload speed: \t%s\n", uploadSpeed)
}
}
Expand Down
24 changes: 11 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,22 @@ const (

func main() {
// Parse flags.
var port int
var seed, tcp *bool
var player *string
var maxConnections int

player = flag.String("player", "", "Open the stream with a video player ("+joinPlayerNames()+")")
flag.IntVar(&port, "port", 8080, "Port to stream the video on")
seed = flag.Bool("seed", false, "Seed after finished downloading")
flag.IntVar(&maxConnections, "conn", 200, "Maximum number of connections")
tcp = flag.Bool("tcp", true, "Allow connections via TCP")
player := flag.String("player", "", "Open the stream with a video player ("+joinPlayerNames()+")")
cfg := NewClientConfig()
flag.IntVar(&cfg.Port, "port", cfg.Port, "Port to stream the video on")
flag.IntVar(&cfg.TorrentPort, "torrent-port", cfg.TorrentPort, "Port to listen for incoming torrent connections")
flag.BoolVar(&cfg.Seed, "seed", cfg.Seed, "Seed after finished downloading")
flag.IntVar(&cfg.MaxConnections, "conn", cfg.MaxConnections, "Maximum number of connections")
flag.BoolVar(&cfg.TCP, "tcp", cfg.TCP, "Allow connections via TCP")
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(exitNoTorrentProvided)
}
cfg.TorrentPath = flag.Arg(0)

// Start up the torrent client.
client, err := NewClient(flag.Arg(0), port, *seed, *tcp, maxConnections)
client, err := NewClient(cfg)
if err != nil {
log.Fatalf(err.Error())
os.Exit(exitErrorInClient)
Expand All @@ -46,7 +44,7 @@ func main() {
// Http handler.
go func() {
http.HandleFunc("/", client.GetFile)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(cfg.Port), nil))
}()

// Open selected video player
Expand All @@ -55,7 +53,7 @@ func main() {
for !client.ReadyForPlayback() {
time.Sleep(time.Second)
}
openPlayer(*player, port)
openPlayer(*player, cfg.Port)
}()
}

Expand Down