rename connwrapper and gophorconn to make more sense

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
kim (grufwub) 2020-05-06 21:08:04 +01:00
parent a6a779aaf0
commit 10c9f02eb3
3 changed files with 37 additions and 26 deletions

39
conn.go
View File

@ -76,61 +76,72 @@ func BeginGophorListen(bindAddr, hostname, port, fwdPort, rootDir string) (*Goph
}
}
func (l *GophorListener) Accept() (*GophorConnWrapper, error) {
func (l *GophorListener) Accept() (*GophorConn, error) {
conn, err := l.Listener.Accept()
if err != nil {
return nil, err
}
connWrapper := new(GophorConnWrapper)
connWrapper.Conn = &GophorConn{ conn }
gophorConn := new(GophorConn)
gophorConn.Conn = &DeadlineConn{ conn }
/* Copy over listener host */
connWrapper.Host = l.Host
connWrapper.Root = l.Root
gophorConn.Host = l.Host
gophorConn.Root = l.Root
/* Should always be ok as listener is type TCP (see above) */
addr, _ := conn.RemoteAddr().(*net.TCPAddr)
connWrapper.Client = &ConnClient{
gophorConn.Client = &ConnClient{
addr.IP.String(),
strconv.Itoa(addr.Port),
}
return connWrapper, nil
return gophorConn, nil
}
type GophorConn struct {
type DeadlineConn struct {
conn net.Conn
}
func (c *GophorConn) Read(b []byte) (int, error) {
func (c *DeadlineConn) Read(b []byte) (int, error) {
/* Implements reader + updates deadline */
c.conn.SetReadDeadline(time.Now().Add(SocketReadTimeout))
return c.conn.Read(b)
}
func (c *GophorConn) Write(b []byte) (int, error) {
func (c *DeadlineConn) Write(b []byte) (int, error) {
/* Implements writer + updates deadline */
c.conn.SetWriteDeadline(time.Now().Add(SocketWriteTimeout))
return c.conn.Write(b)
}
func (c *GophorConn) Close() error {
func (c *DeadlineConn) Close() error {
/* Implements closer */
return c.conn.Close()
}
type GophorConnWrapper struct {
type GophorConn struct {
/* Simple net.Conn wrapper with virtual host and client info */
Conn *GophorConn
Conn *DeadlineConn
Host *ConnHost
Client *ConnClient
Root string
}
func (c *GophorConn) Read(b []byte) (int, error) {
return c.Conn.Read(b)
}
func (c *GophorConnWrapper) RootDir() string {
func (c *GophorConn) Write(b []byte) (int, error) {
return c.Conn.Write(b)
}
func (c *GophorConn) Close() error {
return c.Conn.Close()
}
func (c *GophorConn) RootDir() string {
return c.Root
}

View File

@ -62,13 +62,13 @@ type Request struct {
Parameters []string /* CGI-bin params will be 1 length slice, shell commands populate >=1 */
}
func NewSanitizedRequest(connWrapper *GophorConnWrapper, requestStr string) *Request {
func NewSanitizedRequest(conn *GophorConn, requestStr string) *Request {
/* Split dataStr into request path and parameter string (if pressent) */
relPath, parameters := parseRequestString(requestStr)
relPath = sanitizeRelativePath(connWrapper.RootDir(), relPath)
bufWriter := bufio.NewWriterSize(connWrapper.Conn, SocketWriteBufSize)
requestPath := NewRequestPath(connWrapper.RootDir(), relPath)
return NewRequest(connWrapper.Host, connWrapper.Client, bufWriter, requestPath, parameters)
relPath = sanitizeRelativePath(conn.RootDir(), relPath)
bufWriter := bufio.NewWriterSize(conn.Conn, SocketWriteBufSize)
requestPath := NewRequestPath(conn.RootDir(), relPath)
return NewRequest(conn.Host, conn.Client, bufWriter, requestPath, parameters)
}
func NewRequest(host *ConnHost, client *ConnClient, writer *bufio.Writer, path *RequestPath, parameters []string) *Request {

View File

@ -12,13 +12,13 @@ const (
)
type Worker struct {
ConnWrapper *GophorConnWrapper
Conn *GophorConn
}
func (worker *Worker) Serve() {
defer func() {
/* Close-up shop */
worker.ConnWrapper.Conn.Close()
worker.Conn.Conn.Close()
}()
var count int
@ -32,7 +32,7 @@ func (worker *Worker) Serve() {
endReached := false
for {
/* Buffered read from conn */
count, err = worker.ConnWrapper.Conn.Read(buf)
count, err = worker.Conn.Read(buf)
/* Copy buffer into received string, stop at first tap or CrLf */
for i := 0; i < count; i += 1 {
@ -56,7 +56,7 @@ func (worker *Worker) Serve() {
break
}
Config.SysLog.Error("", "Error reading from socket on port %s: %s\n", worker.ConnWrapper.Host.Port(), err.Error())
Config.SysLog.Error("", "Error reading from socket on port %s: %s\n", worker.Conn.Host.Port(), err.Error())
return
} else if endReached || count < SocketReadBufSize {
/* Reached the end of what we want, break */
@ -79,15 +79,15 @@ func (worker *Worker) Serve() {
switch len(received) {
case lenBefore-4:
/* Send an HTML redirect to supplied URL */
Config.AccLog.Info("("+worker.ConnWrapper.Client.Ip()+") ", "Redirecting to %s\n", received)
worker.ConnWrapper.Conn.Write(generateHtmlRedirect(received))
Config.AccLog.Info("("+worker.Conn.Client.Ip()+") ", "Redirecting to %s\n", received)
worker.Conn.Write(generateHtmlRedirect(received))
return
default:
/* Do nothing */
}
/* Create new request from dataStr */
request := NewSanitizedRequest(worker.ConnWrapper, received)
request := NewSanitizedRequest(worker.Conn, received)
/* Handle request */
gophorErr := Config.FileSystem.HandleRequest(request)