From 773606bd45e283d05a4aaa06787289ad322ca7b7 Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 22 Sep 2016 11:22:10 +1000 Subject: Initial Commit --- .gitignore | 3 +++ .travis.yml | 1 + Dockerfile | 5 +++++ LICENSE | 22 ++++++++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcbcc28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*.bak +gopherproxy diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4f2ee4d --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1dad4b6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM golang:alpine + +EXPOSE 80/tcp + +ENTRYPOINT ["/gopherproxy"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..72873e6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (C) 2016 James Mills + +go-gopher is covered by the MIT license:: + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e4831a --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Gopher (RFC 1436) Web Proxy + +[![Build Status](https://travis-ci.org/prologic/gopherproxy.svg)](https://travis-ci.org/prologic/gopherproxy) + +This is a Gopher (RFC 1436) Web Proxy that acts as a gateway into Gopherspace +by proxying standard Web HTTP requests to Gopher requests of the target server. + +gopherproxy is written in Go (#golang) using the +[go-gopher](https://github.com/prologic/go-gopher) library. + +## Installation + + $ go install github.com/prologic/gopherproxy + +## Usage + +```#!bash +$ gopherproxy +``` + +By default gopherproxy will proxy requests to a locally running Gopher server +on gopher://localhost:70/ -- To change where to proxy to: + +```#!bash +$ gopherproxy -host gopher.floodgap.com +``` + +## License + +MIT diff --git a/main.go b/main.go new file mode 100644 index 0000000..be625c6 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "flag" + "fmt" + "io" + "log" + "net/http" + "strings" + + "github.com/prologic/go-gopher" +) + +var ( + bind = flag.String("bind", ":80", "[int]:port to bind to") + host = flag.String("host", "localhost", "host to proxy to") + port = flag.Int("port", 70, "port to proxy to") +) + +func proxy(res http.ResponseWriter, req *http.Request) { + path := strings.TrimPrefix(req.URL.Path, "/") + + gr, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) + if err != nil { + io.WriteString(res, fmt.Sprintf("Error:
%s
", err)) + return + } + + if gr.Body != nil { + io.Copy(res, gr.Body) + } else { + bytes, err := gr.Dir.ToText() + if err != nil { + io.WriteString(res, fmt.Sprintf("Error:
%s
", err)) + return + } + + io.WriteString(res, string(bytes)) + } +} + +func main() { + flag.Parse() + + http.HandleFunc("/", proxy) + log.Fatal(http.ListenAndServe(*bind, nil)) +} -- cgit v1.2.3-70-g09d2