move struct member usage to instead be struct methods, improve README.md
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
parent
0042ba10c4
commit
8dae2abb6f
67
conn.go
67
conn.go
@ -7,14 +7,43 @@ import (
|
||||
|
||||
type ConnHost struct {
|
||||
/* Hold host specific details */
|
||||
Name string
|
||||
Port string
|
||||
HostName string
|
||||
HostPort string
|
||||
FwdPort string
|
||||
}
|
||||
|
||||
func (host *ConnHost) Name() string {
|
||||
return host.HostName
|
||||
}
|
||||
|
||||
func (host *ConnHost) Port() string {
|
||||
return host.FwdPort
|
||||
}
|
||||
|
||||
func (host *ConnHost) RealPort() string {
|
||||
return host.HostPort
|
||||
}
|
||||
|
||||
func (host *ConnHost) AddrStr() string {
|
||||
return host.Name()+":"+host.Port()
|
||||
}
|
||||
|
||||
type ConnClient struct {
|
||||
/* Hold client specific details */
|
||||
Ip string
|
||||
Port string
|
||||
ClientIp string
|
||||
ClientPort string
|
||||
}
|
||||
|
||||
func (client *ConnClient) Ip() string {
|
||||
return client.ClientIp
|
||||
}
|
||||
|
||||
func (client *ConnClient) Port() string {
|
||||
return client.ClientPort
|
||||
}
|
||||
|
||||
func (client *ConnClient) AddrStr() string {
|
||||
return client.Ip()+":"+client.Port()
|
||||
}
|
||||
|
||||
type GophorListener struct {
|
||||
@ -24,13 +53,13 @@ type GophorListener struct {
|
||||
|
||||
Listener net.Listener
|
||||
Host *ConnHost
|
||||
RootDir string
|
||||
Root string
|
||||
}
|
||||
|
||||
func BeginGophorListen(bindAddr, hostname, port, rootDir string) (*GophorListener, error) {
|
||||
func BeginGophorListen(bindAddr, hostname, port, fwdPort, rootDir string) (*GophorListener, error) {
|
||||
gophorListener := new(GophorListener)
|
||||
gophorListener.Host = &ConnHost{ hostname, port }
|
||||
gophorListener.RootDir = rootDir
|
||||
gophorListener.Host = &ConnHost{ hostname, port, fwdPort }
|
||||
gophorListener.Root = rootDir
|
||||
|
||||
var err error
|
||||
gophorListener.Listener, err = net.Listen("tcp", bindAddr+":"+port)
|
||||
@ -51,8 +80,8 @@ func (l *GophorListener) Accept() (*GophorConn, error) {
|
||||
gophorConn.Conn = conn
|
||||
|
||||
/* Copy over listener host */
|
||||
gophorConn.Host = l.Host
|
||||
gophorConn.RootDir = l.RootDir
|
||||
gophorConn.Host = l.Host
|
||||
gophorConn.Root = l.Root
|
||||
|
||||
/* Should always be ok as listener is type TCP (see above) */
|
||||
addr, _ := conn.RemoteAddr().(*net.TCPAddr)
|
||||
@ -74,7 +103,7 @@ type GophorConn struct {
|
||||
Conn net.Conn
|
||||
Host *ConnHost
|
||||
Client *ConnClient
|
||||
RootDir string
|
||||
Root string
|
||||
}
|
||||
|
||||
func (c *GophorConn) Read(b []byte) (int, error) {
|
||||
@ -89,18 +118,6 @@ func (c *GophorConn) Close() error {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (c *GophorConn) Hostname() string {
|
||||
return c.Host.Name
|
||||
}
|
||||
|
||||
func (c *GophorConn) HostPort() string {
|
||||
return c.Host.Port
|
||||
}
|
||||
|
||||
func (c *GophorConn) HostRoot() string {
|
||||
return c.RootDir
|
||||
}
|
||||
|
||||
func (c *GophorConn) ClientAddr() string {
|
||||
return c.Client.Ip
|
||||
func (c *GophorConn) RootDir() string {
|
||||
return c.Root
|
||||
}
|
||||
|
6
exec.go
6
exec.go
@ -43,9 +43,9 @@ func setupInitialCgiEnviron() []string {
|
||||
func executeCgi(request *Request) *GophorError {
|
||||
/* Get initial CgiEnv variables */
|
||||
cgiEnv := Config.CgiEnv
|
||||
cgiEnv = append(cgiEnv, envKeyValue("SERVER_NAME", request.Host.Name)) /* MUST be set to name of server host client is connecting to */
|
||||
cgiEnv = append(cgiEnv, envKeyValue("SERVER_PORT", request.Host.Port)) /* MUST be set to the server port that client is connecting to */
|
||||
cgiEnv = append(cgiEnv, envKeyValue("REMOTE_ADDR", request.Client.Ip)) /* Remote client addr, MUST be set */
|
||||
cgiEnv = append(cgiEnv, envKeyValue("SERVER_NAME", request.Host.Name())) /* MUST be set to name of server host client is connecting to */
|
||||
cgiEnv = append(cgiEnv, envKeyValue("SERVER_PORT", request.Host.Port())) /* MUST be set to the server port that client is connecting to */
|
||||
cgiEnv = append(cgiEnv, envKeyValue("REMOTE_ADDR", request.Client.Ip())) /* Remote client addr, MUST be set */
|
||||
|
||||
/* We store the query string in Parameters[0]. Ensure we git without initial delimiter */
|
||||
var queryString string
|
||||
|
@ -133,11 +133,11 @@ func listDir(request *Request, hidden map[string]bool) *GophorError {
|
||||
dirContents := make([]byte, 0)
|
||||
|
||||
/* First add a title + a space */
|
||||
dirContents = append(dirContents, buildLine(TypeInfo, "[ "+request.Host.Name+request.SelectorPath()+" ]", "TITLE", NullHost, NullPort)...)
|
||||
dirContents = append(dirContents, buildLine(TypeInfo, "[ "+request.Host.Name()+request.SelectorPath()+" ]", "TITLE", NullHost, NullPort)...)
|
||||
dirContents = append(dirContents, buildInfoLine("")...)
|
||||
|
||||
/* Add a 'back' entry. GoLang Readdir() seems to miss this */
|
||||
dirContents = append(dirContents, buildLine(TypeDirectory, "..", request.PathJoinSelector(".."), request.Host.Name, request.Host.Port)...)
|
||||
dirContents = append(dirContents, buildLine(TypeDirectory, "..", request.PathJoinSelector(".."), request.Host.Name(), request.Host.Port())...)
|
||||
|
||||
/* Walk through files :D */
|
||||
for _, file := range files {
|
||||
@ -151,13 +151,13 @@ func listDir(request *Request, hidden map[string]bool) *GophorError {
|
||||
case file.Mode() & os.ModeDir != 0:
|
||||
/* Directory -- create directory listing */
|
||||
itemPath := request.PathJoinSelector(file.Name())
|
||||
dirContents = append(dirContents, buildLine(TypeDirectory, file.Name(), itemPath, request.Host.Name, request.Host.Port)...)
|
||||
dirContents = append(dirContents, buildLine(TypeDirectory, file.Name(), itemPath, request.Host.Name(), request.Host.Port())...)
|
||||
|
||||
case file.Mode() & os.ModeType == 0:
|
||||
/* Regular file -- find item type and creating listing */
|
||||
itemPath := request.PathJoinSelector(file.Name())
|
||||
itemType := getItemType(itemPath)
|
||||
dirContents = append(dirContents, buildLine(itemType, file.Name(), itemPath, request.Host.Name, request.Host.Port)...)
|
||||
dirContents = append(dirContents, buildLine(itemType, file.Name(), itemPath, request.Host.Name(), request.Host.Port())...)
|
||||
|
||||
default:
|
||||
/* Ignore */
|
||||
|
17
format.go
17
format.go
@ -246,9 +246,20 @@ func formatGophermapFooter(text string, useSeparator bool) []byte {
|
||||
|
||||
/* Replace standard replacement strings */
|
||||
func replaceStrings(str string, connHost *ConnHost) []byte {
|
||||
str = strings.Replace(str, ReplaceStrHostname, connHost.Name, -1)
|
||||
str = strings.Replace(str, ReplaceStrPort, connHost.Port, -1)
|
||||
return []byte(str)
|
||||
/* We only replace the actual host and port values */
|
||||
split := strings.Split(str, Tab)
|
||||
if len(split) < 4 {
|
||||
return []byte(str)
|
||||
}
|
||||
|
||||
split[2] = strings.Replace(split[2], ReplaceStrHostname, connHost.Name(), -1)
|
||||
split[3] = strings.Replace(split[3], ReplaceStrPort, connHost.Port(), -1)
|
||||
|
||||
b := make([]byte, 0)
|
||||
for i := range split {
|
||||
b = append(b, []byte(split[i])...)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
/* Replace new-line characters */
|
||||
|
10
gophor.go
10
gophor.go
@ -58,7 +58,8 @@ func setupServer() []*GophorListener {
|
||||
/* Base server settings */
|
||||
serverRoot := flag.String("root-dir", "/var/gopher", "Change server root directory.")
|
||||
serverHostname := flag.String("hostname", "127.0.0.1", "Change server hostname (FQDN).")
|
||||
serverPort := flag.Int("port", 70, "Change server port (0 to disable unencrypted traffic).")
|
||||
serverPort := flag.Int("port", 70, "Change server bind port.")
|
||||
serverFwdPort := flag.Int("fwd-port", 0, "Change port used in '$port' replacement strings (useful if you're port forwarding).")
|
||||
serverBindAddr := flag.String("bind-addr", "127.0.0.1", "Change server socket bind address")
|
||||
|
||||
/* User supplied caps.txt information */
|
||||
@ -137,7 +138,12 @@ func setupServer() []*GophorListener {
|
||||
|
||||
/* If requested, setup unencrypted listener */
|
||||
if *serverPort != 0 {
|
||||
l, err := BeginGophorListen(*serverBindAddr, *serverHostname, strconv.Itoa(*serverPort), *serverRoot)
|
||||
/* If no forward port set, just use regular */
|
||||
if *serverFwdPort == 0 {
|
||||
*serverFwdPort = *serverPort
|
||||
}
|
||||
|
||||
l, err := BeginGophorListen(*serverBindAddr, *serverHostname, strconv.Itoa(*serverPort), strconv.Itoa(*serverFwdPort), *serverRoot)
|
||||
if err != nil {
|
||||
Config.SysLog.Fatal("", "Error setting up (unencrypted) listener: %s\n", err.Error())
|
||||
}
|
||||
|
@ -60,8 +60,8 @@ type Request struct {
|
||||
func NewSanitizedRequest(conn *GophorConn, requestStr string) *Request {
|
||||
/* Split dataStr into request path and parameter string (if pressent) */
|
||||
relPath, parameters := parseRequestString(requestStr)
|
||||
relPath = sanitizeRelativePath(conn.HostRoot(), relPath)
|
||||
return NewRequest(conn.Host, conn.Client, conn.Conn, NewRequestPath(conn.HostRoot(), relPath), parameters)
|
||||
relPath = sanitizeRelativePath(conn.RootDir(), relPath)
|
||||
return NewRequest(conn.Host, conn.Client, conn.Conn, NewRequestPath(conn.RootDir(), relPath), parameters)
|
||||
}
|
||||
|
||||
func NewRequest(host *ConnHost, client *ConnClient, writer io.Writer, path *RequestPath, parameters []string) *Request {
|
||||
@ -76,12 +76,12 @@ func NewRequest(host *ConnHost, client *ConnClient, writer io.Writer, path *Requ
|
||||
|
||||
func (r *Request) AccessLogInfo(format string, args ...interface{}) {
|
||||
/* You HAVE to be sure that r.Conn is NOT nil before calling this */
|
||||
Config.AccLog.Info("("+r.Client.Ip+") ", format, args...)
|
||||
Config.AccLog.Info("("+r.Client.AddrStr()+") ", format, args...)
|
||||
}
|
||||
|
||||
func (r *Request) AccessLogError(format string, args ...interface{}) {
|
||||
/* You HAVE to be sure that r.Conn is NOT nil before calling this */
|
||||
Config.AccLog.Error("("+r.Client.Ip+") ", format, args...)
|
||||
Config.AccLog.Error("("+r.Client.AddrStr()+") ", format, args...)
|
||||
}
|
||||
|
||||
func (r *Request) WriteRaw(reader io.Reader) *GophorError {
|
||||
|
@ -38,7 +38,7 @@ func (worker *Worker) Serve() {
|
||||
break
|
||||
}
|
||||
|
||||
Config.SysLog.Error("", "Error reading from socket on port %s: %s\n", worker.Conn.Host.Port, err.Error())
|
||||
Config.SysLog.Error("", "Error reading from socket on port %s: %s\n", worker.Conn.Host.Port(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ func (worker *Worker) Serve() {
|
||||
switch len(received) {
|
||||
case lenBefore-4:
|
||||
/* Send an HTML redirect to supplied URL */
|
||||
Config.AccLog.Info("("+worker.Conn.ClientAddr()+") ", "Redirecting to %s\n", received)
|
||||
Config.AccLog.Info("("+worker.Conn.Client.Ip()+") ", "Redirecting to %s\n", received)
|
||||
worker.Conn.Write(generateHtmlRedirect(received))
|
||||
return
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user