Skip to content

Instantly share code, notes, and snippets.

@v4lli
Last active November 12, 2017 12:46
Show Gist options
  • Save v4lli/8d9ba153fc135f98e3390cd65236c64f to your computer and use it in GitHub Desktop.
Save v4lli/8d9ba153fc135f98e3390cd65236c64f to your computer and use it in GitHub Desktop.
// toy http "proxy" (not adhereing the proxy protocol,
// only a pre-configured static host), first go programm.
// replaces some words with l33t alternative and replaces
// all <img> tags.
// call with './proxy www.mmix.cs.hm.edu'; only works for
// http sites, no https.
// known issues:
// - not a real proxy
// - not valid http
// - no error checking \wrt sockets
// - inefficient and DoS'able by downloading large files
package main
import "net"
import "strings"
import "fmt"
import "bufio"
import "io"
import "bytes"
import "os"
import "net/textproto"
import "regexp"
func getHeaders(con net.Conn) []string {
reader := bufio.NewReader(con)
tp := textproto.NewReader(reader)
var headers []string
for {
line, _ := tp.ReadLine()
if len(line) == 0 {
return headers
} else {
headers = append(headers, line)
}
}
}
func prepareRequestHeader(headers []string, host string) []string {
var output []string
for _, element := range headers {
if (strings.HasPrefix(element, "Accept-Encoding:") || strings.HasPrefix(element, "Host:") || strings.HasPrefix(element, "Connection:")) {
fmt.Printf("DROPPING CLIENT HEADER: %v\n", element)
} else {
output = append(output, element)
}
}
output = append(output, "Host: " + host + "\r")
output = append(output, "Connection: close\r")
return append(output, "Accept-Encoding: identity\r")
}
func leetify(b string) string {
img_url := "https://w3-mediapool.hm.edu/mediapool/media/_technik/img/_technik_1/hm_logo_header.de.jpg"
re := regexp.MustCompile("<img (.*)>")
ret := strings.Replace(strings.Replace(strings.Replace(strings.Replace(
strings.Replace(strings.Replace(strings.Replace(strings.Replace(
strings.Replace(strings.Replace(
strings.Replace(strings.Replace(b,
"MMIX", "MM1X", -1),
"Java", "J4v4", -1),
"Computer", "C0mpu73r", -1),
"RISC", "R15C", -1),
"CISC", "C15C", -1),
"Debugger", "D3bu663r", -1),
"Informatik", "1nfr0m471k", -1),
"Studentin", "57ud3n71n", -1),
"Student", "57ud3n7", -1),
"Studierende", "57ud13r3nd3", -1),
"Windows", "w1nd0w5", -1),
"Linux", "l1nux", -1)
return re.ReplaceAllString(ret, "<img src='" + img_url + "'>")
}
func main() {
ln, _ := net.Listen("tcp", "localhost:8080")
dest := os.Args[1]
for {
browser, _ := ln.Accept()
remote, _ := net.Dial("tcp", dest + ":80")
// rewrite request headers, send them to the actual remote server
// line by line
for _, element := range prepareRequestHeader(getHeaders(browser), dest) {
fmt.Printf("> %v\n", element)
fmt.Fprintf(remote, "%v\n", element)
}
fmt.Fprintf(remote, "\r\n\r\n")
fmt.Printf("\r\n\r\n")
var b bytes.Buffer
// buffer the entire response (memory intensive...), leetify it
// and rewrite the <img> tags, then send back to the browser and
// close the connection (OK since we changed to
// "connection: close" above)
io.Copy(&b, remote)
re_content_length := regexp.MustCompile("(Content-Length: [0-9]*\r\n)")
c := re_content_length.ReplaceAllString(b.String(), "")
fmt.Fprintf(browser, "%v", leetify(c))
browser.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment