Skip to content

Commit

Permalink
Enable config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
the0 committed Jul 13, 2020
1 parent b6e8084 commit a5a9b90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ $ "$GOPATH/bin/morty" --help
Morty can be configured using the following environment variables:
- `MORTY_ADDRESS`: Listen address (default to `127.0.0.1:3000`)
- `MORTY_KEY`: HMAC url validation key (base64 encoded) to prevent direct URL opening. Leave blank to disable validation. Use `openssl rand -base64 33` to generate.
- `REPLACE_SITES`: Enable/disable the WIP feature of redirecting sites to privacy respecting alternatives
- `DEBUG`: Enable/disable proxy and redirection logs (default to `true`). Set to `false` to disable.

### Site replacement

Morty can be told to replace sites with alternative ones, in the default `sites.json` we redirect YouTube to Invidious and Twitter to Nitter.

To enable this feature run morty with the `-replace` flag.

### Docker

```
Expand Down
30 changes: 22 additions & 8 deletions morty.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"net/url"
Expand Down Expand Up @@ -38,7 +40,7 @@ const (
const VERSION = "v0.2.0"

var DEBUG = os.Getenv("DEBUG") != "false"
var REPLACE_SITES = os.Getenv("REPLACE_SITES") != "false"
var REPLACE_SITES = false

var CLIENT *fasthttp.Client = &fasthttp.Client{
MaxResponseBodySize: 10 * 1024 * 1024, // 10M
Expand Down Expand Up @@ -98,12 +100,6 @@ var ALLOWED_CONTENTTYPE_PARAMETERS map[string]bool = map[string]bool{
"charset": true,
}

// Replace sites with more trusted/privacy respecting alternatives
var REPLACEMENT_SITES = map[string]string{
"twitter.com": "nitter.net",
"youtube.com": "invidio.us",
}

var UNSAFE_ELEMENTS [][]byte = [][]byte{
[]byte("applet"),
[]byte("canvas"),
Expand Down Expand Up @@ -268,6 +264,8 @@ var MORTY_HTML_PAGE_END string = `

var FAVICON_BYTES []byte

var siteList = make(map[string]string)

func init() {
FaviconBase64 := "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII"

Expand Down Expand Up @@ -889,7 +887,7 @@ func (rc *RequestConfig) ProxifyURI(uri []byte) (string, error) {
}

if REPLACE_SITES {
for site, replace := range REPLACEMENT_SITES {
for site, replace := range siteList {
if u.Host == site {
u.Host = replace
}
Expand Down Expand Up @@ -1006,6 +1004,7 @@ func main() {
version := flag.Bool("version", false, "Show version")
requestTimeout := flag.Uint("timeout", 2, "Request timeout")
socks5 := flag.String("socks5", "", "SOCKS5 proxy")
siteReplace := flag.Bool("replace", false, "Enable experimental site redirection")
flag.Parse()

if *version {
Expand All @@ -1022,6 +1021,21 @@ func main() {
CLIENT.Dial = fasthttpproxy.FasthttpSocksDialer(*socks5)
}

if *siteReplace {
log.Println("Enabling site replacement")

loadedSiteList, err := ioutil.ReadFile("sites.json")
if err != nil {
log.Printf("Failed to read sites.json: %s\n", err)
}

err = json.Unmarshal(loadedSiteList, &siteList)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
REPLACE_SITES = true
}

p := &Proxy{RequestTimeout: time.Duration(*requestTimeout) * time.Second}

if *key != "" {
Expand Down
4 changes: 4 additions & 0 deletions sites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"youtube.com": "indivio.us",
"twitter.com": "nitter.net"
}

0 comments on commit a5a9b90

Please sign in to comment.