Skip to content

Commit

Permalink
Update static file server
Browse files Browse the repository at this point in the history
Now can use `diode publish -http_dir ./test` or `diode publish -http` to
publish port and start static http file server at the same time.

Also add `-indexed` flag to enable/disable the directory indexing.
  • Loading branch information
sc0Vu committed Oct 26, 2020
1 parent 8abc7a5 commit 1d06a63
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SYNOPSYS
COMMANDS
bns Register/Update name service on diode blockchain.
config Manage variables in the local config store.
httpd Enable a public http server as is used by the "diode.link" website
gateway Enable a public http server as is used by the "diode.link" website
publish Publish ports of the local device to the Diode Network.
reset Initialize a new account and a new fleet contract in the network. WARNING deletes current credentials!
socksd Enable a socks proxy for use with browsers and other apps.
Expand Down
4 changes: 2 additions & 2 deletions cmd/diode/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
var (
gatewayCmd = &command.Command{
Name: "gateway",
HelpText: ` Enable a public http server as is used by the "diode.link" website`,
ExampleText: ` diode httpd -httpd_port 8080 -httpsd_port 443 -secure -certpath ./cert.pem -privpath ./priv.pem`,
HelpText: ` Enable a public gateway server as is used by the "diode.link" website`,
ExampleText: ` diode gateway -httpd_port 8080 -httpsd_port 443 -secure -certpath ./cert.pem -privpath ./priv.pem`,
Run: httpdHandler,
Type: command.DaemonCommand,
}
Expand Down
54 changes: 35 additions & 19 deletions cmd/diode/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"github.com/diodechain/diode_go_client/util"
)

const (
httpPort = 80
)

var (
publishCmd = &command.Command{
Name: "publish",
Expand All @@ -37,10 +41,11 @@ func init() {
publishCmd.Flag.StringVar(&cfg.SocksServerHost, "proxy_host", "127.0.0.1", "host of socksd proxy server")
publishCmd.Flag.IntVar(&cfg.SocksServerPort, "proxy_port", 1080, "port of socksd proxy server")
publishCmd.Flag.BoolVar(&cfg.EnableSocksServer, "socksd", false, "enable socksd proxy server")
publishCmd.Flag.BoolVar(&staticServer.Enabled, "httpd", false, "enable httpd static file server")
publishCmd.Flag.StringVar(&staticServer.RootDirectory, "httpd_dir", "/tmp", "the root directory of http static file server")
publishCmd.Flag.StringVar(&staticServer.Host, "httpd_host", "127.0.0.1", "the host of http static file server")
publishCmd.Flag.IntVar(&staticServer.Port, "httpd_port", 80, "the port of http static file server")
publishCmd.Flag.BoolVar(&staticServer.Indexed, "indexed", false, "enable directory indexing in http static file server")
publishCmd.Flag.BoolVar(&staticServer.Enabled, "http", false, "enable http static file server")
publishCmd.Flag.StringVar(&staticServer.RootDirectory, "http_dir", "", "the root directory of http static file server")
publishCmd.Flag.StringVar(&staticServer.Host, "http_host", "127.0.0.1", "the host of http static file server")
publishCmd.Flag.IntVar(&staticServer.Port, "http_port", 8080, "the port of http static file server")
}

var portPattern = regexp.MustCompile(`^(\d+)(:(\d*)(:(tcp|tls|udp))?)?$`)
Expand Down Expand Up @@ -189,6 +194,7 @@ func parseBind(bind string) (*config.Bind, error) {
func publishHandler() (err error) {
cfg := config.AppConfig
portString := make(map[int]*config.Port)

// copy to config
ports, err := parsePorts(cfg.PublicPublishedPorts, config.PublicPublishedMode, cfg.EnableEdgeE2E)
if err != nil {
Expand Down Expand Up @@ -225,6 +231,30 @@ func publishHandler() (err error) {
}
cfg.PublishedPorts = portString

if staticServer.Enabled || len(staticServer.RootDirectory) > 0 {
go func() {
err := staticServer.ListenAndServe()
if err != nil {
if err != http.ErrServerClosed {
printError("Couldn't listen to http: ", err)
}
return
}
}()
app.Defer(func() {
staticServer.Close()
})
// publish the static by default if enabled
if len(cfg.PublishedPorts) == 0 {
cfg.PublishedPorts[httpPort] = &config.Port{
Src: staticServer.Port,
To: httpPort,
Mode: config.PublicPublishedMode,
Protocol: config.AnyProtocol,
}
}
}

if len(cfg.PublishedPorts) == 0 && len(cfg.Binds) == 0 {
fmt.Println()
fmt.Println("ERROR: Can't run publish without any arguments!")
Expand All @@ -242,7 +272,7 @@ func publishHandler() (err error) {
printInfo("")
pool.SetPublishedPorts(cfg.PublishedPorts)
for _, port := range cfg.PublishedPorts {
if port.To == 80 {
if port.To == httpPort {
if port.Mode == config.PublicPublishedMode {
printLabel("HTTP Gateway Enabled", fmt.Sprintf("http://%s.diode.link/", cfg.ClientAddr.HexString()))
}
Expand Down Expand Up @@ -290,20 +320,6 @@ func publishHandler() (err error) {
printLabel(fmt.Sprintf("Port %5d", bind.LocalPort), fmt.Sprintf("%5s %11s:%d", config.ProtocolName(bind.Protocol), bind.To, bind.ToPort))
}
}
if staticServer.Enabled {
go func() {
err := staticServer.ListenAndServe()
if err != nil {
if err != http.ErrServerClosed {
printError("Couldn't listen to http: ", err)
}
return
}
}()
app.Defer(func() {
staticServer.Close()
})
}
app.Wait()
return
}
10 changes: 8 additions & 2 deletions rpc/staticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ func containsDotFile(name string) bool {

type staticFile struct {
http.File
Indexed bool
}

// Readdir is a wrapper around the Readdir method of the embedded File
// Should readdir?!
func (f staticFile) Readdir(n int) (fis []os.FileInfo, err error) {
if !f.Indexed {
return
}
files, err := f.File.Readdir(n)
for _, file := range files {
if !strings.HasPrefix(file.Name(), ".") {
Expand All @@ -43,6 +47,7 @@ func (f staticFile) Readdir(n int) (fis []os.FileInfo, err error) {

type staticFileSystem struct {
http.FileSystem
Indexed bool
}

// Open is a wrapper around the Open method of the embedded FileSystem
Expand All @@ -54,7 +59,7 @@ func (fs staticFileSystem) Open(name string) (http.File, error) {
if err != nil {
return nil, err
}
return staticFile{file}, err
return staticFile{file, fs.Indexed}, err
}

// StaticHTTPServer represents static file server
Expand All @@ -64,13 +69,14 @@ type StaticHTTPServer struct {
RootDirectory string
Host string
Port int
Indexed bool
server *http.Server
cd sync.Once
}

// Handler returns http handler of static file server
func (sv *StaticHTTPServer) Handler() (handler http.Handler) {
fs := staticFileSystem{http.Dir(sv.RootDirectory)}
fs := staticFileSystem{http.Dir(sv.RootDirectory), sv.Indexed}
handler = http.FileServer(fs)
return
}
Expand Down

0 comments on commit 1d06a63

Please sign in to comment.