Files
eephttpd/serve.go

151 lines
3.4 KiB
Go
Raw Permalink Normal View History

2019-09-02 00:23:03 -04:00
package eephttpd
2018-08-24 01:06:39 -04:00
import (
"bytes"
2019-09-02 01:06:57 -04:00
"fmt"
"io/ioutil"
2019-09-13 00:48:30 -04:00
"log"
2019-09-02 00:23:03 -04:00
"net/http"
2019-10-13 15:51:22 -04:00
"os"
2019-09-02 01:06:57 -04:00
"path/filepath"
"strings"
2019-09-13 00:48:30 -04:00
"github.com/d5/tengo/script"
2018-08-24 01:06:39 -04:00
)
func (f *EepHttpd) ProxyRequest(req *http.Request) *http.Request {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil
}
// you can reassign the body if you need to parse it as multipart
// req.Body = ioutil.NopCloser(bytes.NewReader(body))
// create a new url from the raw RequestURI sent by the client
url := fmt.Sprintf("%s://%s:%s/%s", "http", f.SamTracker.Config().TargetHost, f.SamTracker.Config().TargetPort, "a")
log.Println("handling http tracker request", url)
proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))
// We may want to filter some headers, otherwise we could just use a shallow copy
//
proxyReq.Header = req.Header
return proxyReq
}
2019-09-02 00:23:03 -04:00
func (f *EepHttpd) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
2019-10-13 21:38:48 -04:00
rp := f.checkURL(rq)
log.Println("rp", rp)
2021-02-01 11:47:02 -05:00
rw.Header().Set("X-I2P-TORRENTLOCATION", f.magnet)
if rp == "a" {
client := http.Client{}
req := f.ProxyRequest(rq)
resp, err := client.Do(req)
if err != nil {
return
}
resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Fprintf(rw, string(body))
return
}
2019-10-14 00:49:47 -04:00
if strings.HasPrefix(rq.Header.Get("User-Agent"), "git") {
log.Println(rq.Header.Get("User-Agent"))
2019-10-14 00:59:36 -04:00
f.HandleFile(rw, rq)
return
}
2019-10-13 21:38:48 -04:00
if strings.HasSuffix(rp, ".md") {
2019-09-02 01:06:57 -04:00
f.HandleMarkdown(rw, rq)
return
}
2019-10-13 21:38:48 -04:00
if strings.HasSuffix(rp, ".tengo") {
2019-09-02 20:04:20 -04:00
f.HandleScript(rw, rq)
2019-09-13 00:48:30 -04:00
return
2019-09-02 20:04:20 -04:00
}
2019-09-02 01:06:57 -04:00
f.HandleFile(rw, rq)
2018-08-24 01:06:39 -04:00
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if info != nil {
return !info.IsDir()
}
return false
}
2019-09-13 00:48:30 -04:00
func (f *EepHttpd) checkURL(rq *http.Request) string {
p := rq.URL.Path
if strings.HasSuffix("/"+rq.URL.Path, "/a") {
p = "a"
log.Println("URL path", p)
return p
}
2019-10-13 21:38:48 -04:00
if strings.HasSuffix(rq.URL.Path, "/") {
p = filepath.Join(rq.URL.Path, "index.html")
2019-09-13 00:48:30 -04:00
}
2019-10-13 15:51:22 -04:00
if !FileExists(filepath.Join(f.ServeDir, p)) {
2019-10-13 21:38:48 -04:00
p = filepath.Join(rq.URL.Path, "README.md")
2019-10-13 15:51:22 -04:00
}
if !FileExists(filepath.Join(f.ServeDir, p)) {
2019-10-13 21:38:48 -04:00
p = filepath.Join(rq.URL.Path, "index.tengo")
if !FileExists(filepath.Join(f.ServeDir, p)) {
p = rq.URL.Path
2019-10-13 15:51:22 -04:00
}
}
fp := filepath.Join(f.ServeDir, p)
return fp
2019-09-02 20:04:20 -04:00
}
func (f *EepHttpd) HandleScript(rw http.ResponseWriter, rq *http.Request) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 20:04:20 -04:00
bytes, err := ioutil.ReadFile(path)
if err != nil {
2019-09-13 00:48:30 -04:00
log.Println(err)
2019-09-02 20:04:20 -04:00
return
}
2019-09-13 00:48:30 -04:00
scr := script.New(bytes)
com, err := scr.Compile()
2019-09-02 20:04:20 -04:00
if err != nil {
2019-09-13 00:48:30 -04:00
log.Println(err)
2019-09-02 20:04:20 -04:00
panic(err)
}
2019-09-13 00:48:30 -04:00
if err := com.Run(); err != nil {
log.Println(err)
2019-09-02 20:04:20 -04:00
panic(err)
}
2019-09-13 00:48:30 -04:00
response := com.Get("response")
fmt.Fprintf(rw, response.String())
2019-09-02 20:04:20 -04:00
}
2019-09-02 00:23:03 -04:00
func (f *EepHttpd) HandleMarkdown(rw http.ResponseWriter, rq *http.Request) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 01:06:57 -04:00
bytes, err := ioutil.ReadFile(path)
if err != nil {
return
}
f.mark.Render(rw, bytes)
}
2019-10-14 00:38:22 -04:00
func (f *EepHttpd) HandleGit(rw http.ResponseWriter, rq *http.Request) {
log.Println("Handling Git")
f.Server.ServeHTTP(rw, rq)
2019-10-14 00:38:22 -04:00
}
2019-09-02 01:06:57 -04:00
func (f *EepHttpd) HandleFile(rw http.ResponseWriter, rq *http.Request) {
2019-09-02 20:04:20 -04:00
path := f.checkURL(rq)
2019-09-02 01:06:57 -04:00
bytes, err := ioutil.ReadFile(path)
if err != nil {
f.HandleMissing(rw, rq)
}
fmt.Fprintf(rw, string(bytes))
}
2018-08-24 01:06:39 -04:00
2019-09-02 01:06:57 -04:00
func (f *EepHttpd) HandleMissing(rw http.ResponseWriter, rq *http.Request) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 20:04:20 -04:00
fmt.Fprintf(rw, "ERROR %s NOT FOUND", strings.Replace(path, f.ServeDir, "", -1))
2018-08-24 01:06:39 -04:00
}