This commit is contained in:
yourok
2018-08-29 12:33:14 +03:00
commit 0ca43a2c4d
54 changed files with 5669 additions and 0 deletions

90
src/main/main.go Normal file
View File

@@ -0,0 +1,90 @@
package main
import (
"bytes"
"errors"
"fmt"
"net/http"
"os"
"time"
"github.com/alexflint/go-arg"
"server"
"server/settings"
"server/version"
)
type args struct {
Port string `arg:"-p" help:"web server port"`
Path string `arg:"-d" help:"database path"`
Add string `arg:"-a" help:"add torrent link and exit"`
Kill bool `arg:"-k" help:"dont kill program on signal"`
}
func (args) Version() string {
return "TorrServer " + version.Version
}
var params args
func main() {
//test()
//return
//for _, g := range tmdb.GetMovieGenres("ru") {
// fmt.Println(g.Name, g.ID)
//}
//return
//movs, _ := tmdb.DiscoverShows(map[string]string{}, 1)
//js, _ := json.MarshalIndent(movs, "", " ")
//fmt.Println(string(js))
//return
arg.MustParse(&params)
if params.Path == "" {
params.Path, _ = os.Getwd()
}
if params.Port == "" {
params.Port = "8090"
}
if params.Add != "" {
add()
}
Preconfig(params.Kill)
server.Start(params.Path, params.Port)
settings.SaveSettings()
fmt.Println(server.WaitServer())
time.Sleep(time.Second * 3)
os.Exit(0)
}
func add() {
err := addRemote()
if err != nil {
fmt.Println("Error add torrent:", err)
os.Exit(-1)
}
fmt.Println("Added ok")
os.Exit(0)
}
func addRemote() error {
url := "http://localhost:" + params.Port + "/torrent/add"
fmt.Println("Add torrent link:", params.Add, "\n", url)
json := `{"Link":"` + params.Add + `"}`
resp, err := http.Post(url, "text/html; charset=utf-8", bytes.NewBufferString(json))
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return nil
}

49
src/main/preconfig_pos.go Normal file
View File

@@ -0,0 +1,49 @@
// +build !windows
package main
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"syscall"
)
func Preconfig(kill bool) {
if kill {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGSTOP,
syscall.SIGPIPE,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
for s := range sigc {
fmt.Println("Signal catched:", s)
fmt.Println("For stop server, close in web")
}
}()
}
//dns resover
addrs, err := net.LookupHost("www.themoviedb.org")
if len(addrs) == 0 {
fmt.Println("Check dns", addrs, err)
fn := func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", "1.1.1.1:53")
}
net.DefaultResolver = &net.Resolver{
Dial: fn,
}
addrs, err = net.LookupHost("www.themoviedb.org")
fmt.Println("Check new dns", addrs, err)
}
}

View File

@@ -0,0 +1,7 @@
// +build windows
package main
func Preconfig(kill bool) {
}

74
src/main/test.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"fmt"
"log"
"os"
"sync"
"time"
"server/utils"
"github.com/anacrolix/torrent"
)
func test() {
config := torrent.NewDefaultClientConfig()
config.EstablishedConnsPerTorrent = 100
config.HalfOpenConnsPerTorrent = 65
config.DisableIPv6 = true
config.NoDHT = true
client, err := torrent.NewClient(config)
if err != nil {
log.Fatal(err)
}
//Ubuntu
t, err := client.AddMagnet("magnet:?xt=urn:btih:e4be9e4db876e3e3179778b03e906297be5c8dbe&dn=ubuntu-18.04-desktop-amd64.iso&tr=http%3a%2f%2ftorrent.ubuntu.com%3a6969%2fannounce&tr=http%3a%2f%2fipv6.torrent.ubuntu.com%3a6969%2fannounce")
if err != nil {
log.Fatal(err)
}
<-t.GotInfo()
file := t.Files()[0]
reader := file.NewReader()
var wa sync.WaitGroup
wa.Add(1)
go func() {
buf := make([]byte, t.Info().PieceLength)
for {
_, err := reader.Read(buf)
if err != nil {
break
}
}
wa.Done()
}()
go func() {
cl := t.Closed()
lastTimeSpeed := time.Now()
DownloadSpeed := 0.0
BytesReadUsefulData := int64(0)
for {
select {
case <-cl:
return
default:
client.WriteStatus(os.Stdout)
st := t.Stats()
deltaDlBytes := st.BytesReadUsefulData.Int64() - BytesReadUsefulData
deltaTime := time.Since(lastTimeSpeed).Seconds()
DownloadSpeed = float64(deltaDlBytes) / deltaTime
BytesReadUsefulData = st.BytesReadUsefulData.Int64()
lastTimeSpeed = time.Now()
fmt.Println("DL speed:", utils.Format(DownloadSpeed))
}
time.Sleep(time.Second)
}
}()
wa.Wait()
}