-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
149 lines (124 loc) · 2.84 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/cheggaaa/pb/v3"
"github.com/spf13/cobra"
)
const (
DefaultPathDir = "tmp"
DefaultServerPort = 8088
DefaultWebPort = 8089
)
var (
port, webport int
path, secret string
)
func ServerCmd() *cobra.Command {
command := &cobra.Command{
Use: "server",
Run: func(cmd *cobra.Command, args []string) {
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
initCommand()
go func() {
runHttpServer(webport)
}()
go func() {
runTcpServer(port, path)
}()
<-quit
},
}
command.Flags().StringVarP(&path, "path", "", "", "File save path")
command.Flags().StringVarP(&secret, "secret", "", "", "Http transmission key")
command.Flags().IntVarP(&port, "port", "", 0, "Tcp server port")
command.Flags().IntVarP(&webport, "webport", "", 0, "Http server port")
return command
}
func initCommand() {
FetchDeviceInfo()
if path == "" {
path = DefaultPathDir
}
if port == 0 {
port = DefaultServerPort
}
if webport == 0 {
webport = DefaultWebPort
}
_ = os.Mkdir(fmt.Sprintf("%s/", path), os.ModePerm)
}
func runTcpServer(port int, savePath string) {
fn := "runTcpServer"
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Printf("%s starting server err:%s \n", fn, err)
return
}
defer func() {
_ = listener.Close()
}()
fmt.Printf("%s is listening on port %d \n", fn, port)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("%s connection error:%s \n", fn, err)
continue
}
go handleConnection(conn, savePath)
}
}
func handleConnection(conn net.Conn, savePath string) {
fn := "handleConnection"
defer func() {
_ = conn.Close()
}()
reader := bufio.NewReader(conn)
meta, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("%s reading file metadata:%s \n", fn, err)
return
}
meta = strings.TrimSpace(meta)
parts := strings.Split(meta, "|")
if len(parts) != 2 {
fmt.Printf("%s invalid metadata received", fn)
return
}
fileName := parts[0]
fileSize := 0
if _, err = fmt.Sscanf(parts[1], "%d", &fileSize); err != nil {
fmt.Printf("%s parsing file size err:%s \n", fn, err)
return
}
fullPath := filepath.Join(savePath, fileName)
if err = os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
fmt.Printf("%s creating directories err:%s \n", fn, err)
return
}
f, err := os.Create(fullPath)
if err != nil {
fmt.Printf("%s creating file:%s \n", fn, err)
return
}
defer func() {
_ = f.Close()
}()
bar := pb.Start64(int64(fileSize))
bar.Set(pb.Bytes, true)
defer bar.Finish()
proxyReader := bar.NewProxyReader(reader)
if _, err = io.Copy(f, proxyReader); err != nil {
fmt.Printf("%s saving file:%s", fn, err)
return
}
fmt.Printf("File received:%s", fullPath)
}