7 Commits
v0.4 ... v0.6

Author SHA1 Message Date
bel
c4c37068f3 New oauth2client 2019-12-31 11:21:15 -07:00
bel
d71b00e067 Fix 2019-11-06 19:00:56 -07:00
Bel LaPointe
d98703610d remove unused and rename 2019-11-03 07:56:43 -07:00
Bel LaPointe
01b7b06971 Optional oauth via + flag 2019-11-03 07:55:38 -07:00
Bel LaPointe
7d3d6d88f6 Re-enable config files 2019-11-03 07:45:23 -07:00
Bel LaPointe
8c415f2a39 Update to scoped oauth 2019-11-02 08:03:35 -06:00
Bel LaPointe
df0232e24c Create dockerfile from dockerize.do and test WS 2019-11-02 07:26:58 -06:00
10 changed files with 161 additions and 24 deletions

4
.gitignore vendored
View File

@@ -1,6 +1,10 @@
lz4
rclone
rcloner
exec
exec-*
**/exec
**/exec-*
Go
cloudly
dockfile

16
Dockerfile Executable file
View File

@@ -0,0 +1,16 @@
FROM golang:1.13-alpine as certs
RUN apk update && apk add --no-cache ca-certificates
FROM busybox:glibc
RUN mkdir -p /var/log
WORKDIR /main
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
COPY . .
ENV GOPATH=""
ENV MNT="/mnt/"
ENTRYPOINT ["/main/exec-rproxy3"]
CMD []

View File

@@ -1,11 +0,0 @@
p: 54243
r:
- echo:http://localhost:49982
- echo2:http://192.168.0.86:38090
#crt: ./testdata/rproxy3server.crt
#key: ./testdata/rproxy3server.key
#user: bel
#pass: bel
rate: 1
burst: 2
timeout: 10

8
config/config.go Normal file → Executable file
View File

@@ -2,13 +2,13 @@ package config
import (
"fmt"
"log"
"strings"
"time"
)
type Proxy struct {
To string
BOAuthZ bool
To string
}
func parseProxy(s string) (string, Proxy) {
@@ -21,9 +21,6 @@ func parseProxy(s string) (string, Proxy) {
if len(l) > 1 {
p.To = l[1]
}
if len(l) > 2 {
p.BOAuthZ = l[2] == "true"
}
return key, p
}
@@ -46,6 +43,7 @@ func GetPort() string {
func GetRate() (int, int) {
rate := conf.Get("r").GetInt()
burst := conf.Get("b").GetInt()
log.Println("rate/burst:", rate, burst)
return rate, burst
}

8
config/new.go Normal file → Executable file
View File

@@ -30,7 +30,11 @@ func Refresh() error {
}
func parseArgs() (*args.ArgSet, error) {
as := args.NewArgSet()
configFiles := []string{}
if v, ok := os.LookupEnv("CONFIG"); ok {
configFiles = strings.Split(v, ",")
}
as := args.NewArgSet(configFiles...)
as.Append(args.STRING, "user", "username for basic auth", "")
as.Append(args.STRING, "pass", "password for basic auth", "")
@@ -41,7 +45,7 @@ func parseArgs() (*args.ArgSet, error) {
as.Append(args.STRING, "key", "path to key for ssl", "")
as.Append(args.STRING, "tcp", "address for tcp only tunnel", "")
as.Append(args.DURATION, "timeout", "timeout for tunnel", time.Minute)
as.Append(args.STRING, "proxy", "double-comma separated from,scheme://to.tld:port,oauth,,", "")
as.Append(args.STRING, "proxy", "double-comma separated (+ if oauth)from,scheme://to.tld:port,oauth,,", "")
as.Append(args.STRING, "oauth", "url for boauthz", "")
err := as.Parse()

11
example_config.yaml Executable file
View File

@@ -0,0 +1,11 @@
user: ""
pass: ""
port: 51555
r: 100
b: 100
crt: ""
key: ""
tcp: ""
timeout: 1m
proxy: a,http://localhost:41912,,+b,http://localhost:41912
oauth: http://localhost:23456

View File

@@ -52,7 +52,7 @@ func (s *Server) lookup(host string) (*url.URL, error) {
func (s *Server) lookupBOAuthZ(host string) (bool, error) {
v := packable.NewString()
err := s.db.Get(nsBOAuthZ, host, v)
return v.String() != "", err
return v.String() == "true", err
}
func mapKey(host string) string {

View File

@@ -53,12 +53,14 @@ type Server struct {
}
func (s *Server) Route(src string, dst config.Proxy) error {
hasOAuth := strings.HasPrefix(src, "+")
src = strings.TrimPrefix(src, "+")
log.Printf("Adding route %q -> %v...\n", src, dst)
u, err := url.Parse(dst.To)
if err != nil {
return err
}
s.db.Set(nsBOAuthZ, src, packable.NewString(fmt.Sprint(dst.BOAuthZ)))
s.db.Set(nsBOAuthZ, src, packable.NewString(fmt.Sprint(hasOAuth)))
return s.db.Set(nsRouting, src, packable.NewURL(u))
}
@@ -114,13 +116,14 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
return
}
}
ok, err := s.lookupBOAuthZ(mapKey(r.Host))
key := mapKey(r.Host)
ok, err := s.lookupBOAuthZ(key)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if boauthz, useoauth := config.GetBOAuthZ(); ok && useoauth {
err := oauth2client.Authenticate(boauthz, w, r)
if url, exists := config.GetBOAuthZ(); ok && exists {
err := oauth2client.Authenticate(url, key, w, r)
if err != nil {
return
}
@@ -161,7 +164,7 @@ func pipe(a, b net.Conn) {
func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, can := context.WithTimeout(r.Context(), time.Second*time.Duration(config.GetTimeout()))
ctx, can := context.WithTimeout(r.Context(), time.Duration(config.GetTimeout()))
defer can()
if err := s.limiter.Wait(ctx); err != nil {
w.WriteHeader(http.StatusTooManyRequests)

36
testdata/index.html vendored Executable file
View File

@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Go WebSocket Tutorial</title>
</head>
<body>
<h2>Hello World</h2>
<script>
let socket = new WebSocket("ws://a.bel.test:51555/ws");
document.getElementsByTagName("body")[0].innerHTML += "<br>connecting";
socket.onopen = () => {
document.getElementsByTagName("body")[0].innerHTML += "<br>connected";
socket.send("Hi From the Client!")
};
socket.onclose = event => {
document.getElementsByTagName("body")[0].innerHTML += "<br>disconnected";
socket.send("Client Closed!")
};
socket.onerror = error => {
document.getElementsByTagName("body")[0].innerHTML += "<br>error:" + error;
console.log("Socket Error: ", error);
};
socket.onmessage = function(msgevent) {
document.getElementsByTagName("body")[0].innerHTML += "<br>got:" + msgevent.data;
};
</script>
</body>
</html>

76
testdata/ws.go vendored Executable file
View File

@@ -0,0 +1,76 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
func homePage(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadFile("./index.html")
fmt.Fprintf(w, "%s", b)
}
func setupRoutes() {
http.HandleFunc("/", homePage)
http.HandleFunc("/ws", wsEndpoint)
}
func main() {
fmt.Println("Hello World")
setupRoutes()
log.Fatal(http.ListenAndServe(":8080", nil))
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func reader(conn *websocket.Conn) {
for {
// read in a message
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Println(err)
return
}
// print out that message for clarity
fmt.Println(string(p))
if err := conn.WriteMessage(messageType, p); err != nil {
log.Println(err)
return
}
}
}
func wsEndpoint(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// upgrade this connection to a WebSocket
// connection
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}
log.Println("Client Connected")
// listen indefinitely for new messages coming
// through on our WebSocket connection
go reader(ws)
for {
log.Println("writing...")
err = ws.WriteMessage(1, []byte("Hi Client!"))
log.Println("written")
if err != nil {
log.Println(err)
return
}
time.Sleep(time.Second)
}
}