Improve server. (#103)

This commit is contained in:
Antonio Navarro Perez 2021-12-09 17:45:25 +01:00 committed by GitHub
parent 4444a44b7f
commit e90e6eaf1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 9 deletions

View file

@ -116,7 +116,12 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error {
return fmt.Errorf("error starting item store: %w", err)
}
c, err := torrent.NewClient(st, fis, conf.Torrent)
id, err := torrent.GetOrCreatePeerID(filepath.Join(conf.Torrent.MetadataFolder, "ID"))
if err != nil {
return fmt.Errorf("error creating node ID: %w", err)
}
c, err := torrent.NewClient(st, fis, conf.Torrent, id)
if err != nil {
return fmt.Errorf("error starting torrent client: %w", err)
}

View file

@ -13,11 +13,11 @@ import (
"github.com/rs/zerolog/log"
)
func NewClient(st storage.ClientImpl, fis bep44.Store, cfg *config.TorrentGlobal) (*torrent.Client, error) {
func NewClient(st storage.ClientImpl, fis bep44.Store, cfg *config.TorrentGlobal, id [20]byte) (*torrent.Client, error) {
// TODO download and upload limits
torrentCfg := torrent.NewDefaultClientConfig()
torrentCfg.Seed = true
// torrentCfg.DisableWebseeds = true
torrentCfg.PeerID = string(id[:])
torrentCfg.DefaultStorage = st
torrentCfg.DisableIPv6 = cfg.DisableIPv6

30
torrent/id.go Normal file
View file

@ -0,0 +1,30 @@
package torrent
import (
"crypto/rand"
"os"
)
var emptyBytes [20]byte
func GetOrCreatePeerID(p string) ([20]byte, error) {
idb, err := os.ReadFile(p)
if err == nil {
var out [20]byte
copy(out[:], idb)
return out, nil
}
if !os.IsNotExist(err) {
return emptyBytes, err
}
var out [20]byte
_, err = rand.Read(out[:])
if err != nil {
return emptyBytes, err
}
return out, os.WriteFile(p, out[:], 0755)
}

View file

@ -94,12 +94,14 @@ func (s *Server) Start() error {
}
s.fw = w
go func() {
if err := s.makeMagnet(); err != nil {
return err
s.updateState(ERROR)
s.log.Error().Err(err).Msg("error generating magnet on start")
}
go s.watch()
s.watch()
}()
go func() {
for {
@ -149,7 +151,7 @@ func (s *Server) makeMagnet() error {
s.log.Info().Msg("starting serving new torrent")
info := metainfo.Info{
PieceLength: 1 << 8,
PieceLength: 2 << 18,
}
s.updateState(READING)