use vfsgen for sql file
This commit is contained in:
parent
527af9fa98
commit
c9c47b9105
|
@ -1,20 +1,31 @@
|
|||
.env
|
||||
.env.*
|
||||
*.bak
|
||||
certs
|
||||
acme.d
|
||||
xversion.go
|
||||
assets_vfsdata.go
|
||||
|
||||
mgmt-server-linux
|
||||
mgmt-server-macos
|
||||
mgmt-server-windows-debug.exe
|
||||
mgmt-server-windows.exe
|
||||
|
||||
telebit-client-linux
|
||||
telebit-client-macos
|
||||
telebit-client-windows-debug.exe
|
||||
telebit-client-windows.exe
|
||||
|
||||
/mplexer/cmd/dnsclient/dnsclient
|
||||
/mplexer/cmd/mgmt/mgmt
|
||||
/mplexer/cmd/sqlstore/sqlstore
|
||||
/mplexer/mgmt/cmd/mgmt/mgmt
|
||||
/mplexer/cmd/signjwt/signjwt
|
||||
/mplexer/cmd/telebit/telebit
|
||||
|
||||
/telebit
|
||||
/cmd/telebit/telebit
|
||||
/telebitd
|
||||
/cmd/telebitd/telebitd
|
||||
/telebit-relay
|
||||
/cmd/telebit-relay/telebit-relay
|
||||
*.exe
|
||||
|
||||
.*.sw*
|
||||
log.txt
|
||||
|
|
|
@ -3,12 +3,21 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.coolaj86.com/coolaj86/go-telebitd/mplexer/mgmt/authstore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
store, err := authstore.NewStore(nil)
|
||||
connStr := "postgres://postgres:postgres@localhost:5432/postgres"
|
||||
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
|
||||
connStr += "?sslmode=disable"
|
||||
} else {
|
||||
connStr += "?sslmode=required"
|
||||
}
|
||||
initSQL := "./init.sql"
|
||||
|
||||
store, err := authstore.NewStore(connStr, initSQL)
|
||||
if nil != err {
|
||||
log.Fatal("connection error", err)
|
||||
return
|
||||
|
|
|
@ -69,7 +69,7 @@ func main() {
|
|||
|
||||
if len(os.Args) >= 2 {
|
||||
if "version" == os.Args[1] {
|
||||
fmt.Printf("telebit %s %s %s %s", GitVersion, GitRev[:7], GitTimestamp)
|
||||
fmt.Printf("telebit %s %s %s", GitVersion, GitRev[:7], GitTimestamp)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
// +build !dev
|
||||
//go:generate go run -mod vendor github.com/shurcooL/vfsgen/cmd/vfsgendev -source="git.coolaj86.com/coolaj86/go-telebitd/mplexer/files".Assets
|
||||
|
||||
package files
|
|
@ -0,0 +1,20 @@
|
|||
CREATE extension IF NOT EXISTS pgcrypto;
|
||||
SET TIMEZONE='UTC';
|
||||
|
||||
--DROP TABLE IF EXISTS authorizations;
|
||||
CREATE TABLE IF NOT EXISTS authorizations (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
slug TEXT NOT NULL,
|
||||
shared_key TEXT NOT NULL,
|
||||
public_key TEXT NOT NULL,
|
||||
machine_ppid TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
|
||||
deleted_at TIMESTAMP NOT NULL DEFAULT ('epoch' AT TIME ZONE 'UTC')
|
||||
);
|
||||
|
||||
--CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_slug ON authorizations (slug);
|
||||
CREATE INDEX IF NOT EXISTS idx_slug ON authorizations (slug);
|
||||
CREATE INDEX IF NOT EXISTS idx_machine_ppid ON authorizations (machine_ppid);
|
||||
CREATE INDEX IF NOT EXISTS idx_public_key ON authorizations (public_key);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// +build dev
|
||||
|
||||
package files
|
||||
|
||||
import "net/http"
|
||||
|
||||
var Assets http.FileSystem = http.Dir("assets")
|
|
@ -0,0 +1,17 @@
|
|||
package files
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Open(pathstr string) (http.File, error) {
|
||||
f, err := Assets.Open(pathstr)
|
||||
if nil != err {
|
||||
f, err = os.Open(pathstr)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package telebit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDialServer(t *testing.T) {
|
||||
// TODO replace the websocket connection with a mock server
|
||||
|
||||
//ctx := context.Background()
|
||||
tun := &WebsocketTunnel{}
|
||||
|
||||
mux := NewRouteMux()
|
||||
t.Fatal(ListenAndServe(tun, mux))
|
||||
}
|
||||
|
||||
var ErrNoImpl error = errors.New("not implemented")
|
||||
|
||||
// RWTest is a fake buffer
|
||||
type RWTest struct {
|
||||
closed bool
|
||||
tmpr []byte
|
||||
}
|
||||
|
||||
func (rwt *RWTest) Read(dst []byte) (int, error) {
|
||||
if rwt.closed {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
id := Addr{
|
||||
scheme: "http",
|
||||
addr: "192.168.1.108",
|
||||
port: 6732,
|
||||
}
|
||||
tun := Addr{
|
||||
scheme: "http",
|
||||
termination: TLS,
|
||||
addr: "abc.example.com",
|
||||
port: 443,
|
||||
}
|
||||
|
||||
if 0 == len(rwt.tmpr) {
|
||||
b := []byte("Hello, World!")
|
||||
h, _, _ := Encode(b, id, tun)
|
||||
rwt.tmpr = append(h, b...)
|
||||
}
|
||||
|
||||
n := copy(dst, rwt.tmpr)
|
||||
rwt.tmpr = rwt.tmpr[n:]
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (rwt *RWTest) Write(int, []byte) error {
|
||||
if rwt.closed {
|
||||
return io.EOF
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rwt *RWTest) Close() error {
|
||||
rwt.closed = true
|
||||
return nil
|
||||
}
|
|
@ -10,7 +10,7 @@ func TestStore(t *testing.T) {
|
|||
// Note: test output is cached (running twice will not result in two records)
|
||||
|
||||
connStr := "postgres://postgres:postgres@localhost/postgres"
|
||||
if strings.Contains(connStr, "@localhost/") {
|
||||
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
|
||||
connStr += "?sslmode=disable"
|
||||
} else {
|
||||
connStr += "?sslmode=required"
|
||||
|
|
|
@ -9,15 +9,23 @@ import (
|
|||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"git.coolaj86.com/coolaj86/go-telebitd/mplexer/files"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
// pq injects itself into sql as 'postgres'
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func NewStore(pgURL, initSQL string) (Store, error) {
|
||||
// https://godoc.org/github.com/lib/pq
|
||||
|
||||
f, err := files.Open(initSQL)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbtype := "postgres"
|
||||
sqlBytes, err := ioutil.ReadFile(initSQL)
|
||||
sqlBytes, err := ioutil.ReadAll(f)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ func main() {
|
|||
|
||||
connStr := *dbURL
|
||||
// TODO url.Parse
|
||||
if strings.Contains(connStr, "@localhost/") {
|
||||
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
|
||||
connStr += "?sslmode=disable"
|
||||
} else {
|
||||
connStr += "?sslmode=required"
|
||||
|
@ -100,7 +100,7 @@ func main() {
|
|||
|
||||
bind := *addr + ":" + *port
|
||||
fmt.Println("Listening on", bind)
|
||||
fmt.Fprintf(os.Stderr, "failed:", http.ListenAndServe(bind, routeAll()))
|
||||
fmt.Fprintf(os.Stderr, "failed: %s", http.ListenAndServe(bind, routeAll()))
|
||||
}
|
||||
|
||||
// newDuckDNSProvider is for the sake of demoing the tunnel
|
||||
|
|
|
@ -105,6 +105,8 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
|||
|
||||
fmt.Println("[debug] forwarding tcp connection")
|
||||
var err error = nil
|
||||
|
||||
ForwardData:
|
||||
for {
|
||||
select {
|
||||
// TODO do we need a context here?
|
||||
|
@ -115,35 +117,35 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
|||
_, err = target.Write(b)
|
||||
if nil != err {
|
||||
fmt.Printf("write to target failed: %q\n", err.Error())
|
||||
break
|
||||
break ForwardData
|
||||
}
|
||||
case b := <-dstCh:
|
||||
target.SetDeadline(time.Now().Add(timeout))
|
||||
_, err = client.Write(b)
|
||||
if nil != err {
|
||||
fmt.Printf("write to remote failed: %q\n", err.Error())
|
||||
break
|
||||
break ForwardData
|
||||
}
|
||||
case err = <-srcErrCh:
|
||||
if nil == err {
|
||||
break
|
||||
break ForwardData
|
||||
}
|
||||
if io.EOF != err {
|
||||
fmt.Printf("read from remote client failed: %q\n", err.Error())
|
||||
} else {
|
||||
fmt.Printf("Connection closed (possibly by remote client)\n")
|
||||
}
|
||||
break
|
||||
break ForwardData
|
||||
case err = <-dstErrCh:
|
||||
if nil == err {
|
||||
break
|
||||
break ForwardData
|
||||
}
|
||||
if io.EOF != err {
|
||||
fmt.Printf("read from local target failed: %q\n", err.Error())
|
||||
} else {
|
||||
fmt.Printf("Connection closed (possibly by local target)\n")
|
||||
}
|
||||
break
|
||||
break ForwardData
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
//+build tools
|
||||
// +build tools
|
||||
|
||||
// tools is a faux package for tracking dependencies that don't make it into the code
|
||||
package tools
|
||||
|
||||
import _ "git.rootprojects.org/root/go-gitver"
|
||||
import (
|
||||
_ "git.rootprojects.org/root/go-gitver"
|
||||
_ "github.com/shurcooL/vfsgen"
|
||||
_ "github.com/shurcooL/vfsgen/cmd/vfsgendev"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue