forked from gokyle/webshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
96 lines (82 loc) · 1.88 KB
/
server.go
File metadata and controls
96 lines (82 loc) · 1.88 KB
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
package webshell
import (
"crypto/tls"
"log"
"net/http"
"time"
)
const WEBSHELL_VERSION = "2.1.0"
var (
ReadTimeout = 3 * time.Second
WriteTimeout = 3 * time.Second
)
type WebApp struct {
name string
host string
port string
key string
cert string
srv *http.Server
mux *http.ServeMux
}
// Retrieve the app's name.
func (app *WebApp) Name() string {
return app.name
}
// Retrieve the app's host.
func (app *WebApp) Host() string {
return app.host
}
// Retrieve the app's port.
func (app *WebApp) Port() string {
return app.port
}
// Retrieve the address (host + port) the app will serve on.
func (app *WebApp) Address() string {
return serverAddress(app.host, app.port)
}
// IsTLS returns true if the app is configured for TLS.
func (app *WebApp) IsTLS() bool {
return app.srv.TLSConfig != nil
}
func serverAddress(host, port string) string {
if host == "" {
return ":" + port
} else {
return host + ":" + port
}
return ""
}
// NewApp creates a new web app.
func NewApp(name, host, port string) *WebApp {
mux := http.NewServeMux()
srv := &http.Server{
Addr: serverAddress(host, port),
Handler: mux,
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
MaxHeaderBytes: 1 << 20,
}
app := WebApp{name, host, port, "", "", srv, mux}
return &app
}
// NewTLSApp creates a new app that will serve TLS.
func NewTLSApp(name, host, port, key, cert string) *WebApp {
app := NewApp(name, host, port)
app.key = key
app.cert = cert
app.srv.TLSConfig = new(tls.Config)
return app
}
// Serve enables the web server; if it is configured for TLS, it will
// listen for and serve TLS requests. If not, it will serve standard
// HTTP requests.
func (app *WebApp) Serve() error {
log.Println("now listening on ", app.Address())
if app.IsTLS() {
return app.srv.ListenAndServeTLS(app.cert, app.key)
} else {
return app.srv.ListenAndServe()
}
return nil
}