[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [check/master] Start adding an ip endpoint to the api
commit 727327d4f1f3ecd5209f5ea2e827676019c56459
Author: Arlo Breault <arlolra@xxxxxxxxx>
Date: Sun Mar 23 14:10:00 2014 -0700
Start adding an ip endpoint to the api
* This is trac 11270 and github #23
---
check.go | 3 ++-
handlers.go | 27 ++++++++++++++++++---------
utils.go | 15 +++++++++++++++
3 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/check.go b/check.go
index 00b25b6..27277ba 100644
--- a/check.go
+++ b/check.go
@@ -60,9 +60,10 @@ func main() {
// routes
http.HandleFunc("/", RootHandler(CompileTemplate(*basePath, domain, "index.html"), exits, domain, Phttp, Locales))
bulk := BulkHandler(CompileTemplate(*basePath, domain, "bulk.html"), exits, domain)
- http.HandleFunc("/api/bulk", bulk)
http.HandleFunc("/torbulkexitlist", bulk)
http.HandleFunc("/cgi-bin/TorBulkExitList.py", bulk)
+ http.HandleFunc("/api/bulk", bulk)
+ http.HandleFunc("/api/ip", APIHandler(exits))
// start the server
log.Printf("Listening on port: %d\n", *port)
diff --git a/handlers.go b/handlers.go
index be9bd6a..27b6e40 100644
--- a/handlers.go
+++ b/handlers.go
@@ -2,6 +2,7 @@ package main
import (
"bytes"
+ "encoding/json"
"fmt"
"github.com/samuel/go-gettext/gettext"
"html/template"
@@ -11,7 +12,6 @@ import (
"net/http"
"regexp"
"strconv"
- "strings"
"time"
)
@@ -44,14 +44,7 @@ func RootHandler(Layout *template.Template, Exits *Exits, domain *gettext.Domain
)
// get remote ip
- host := r.Header.Get("X-Forwarded-For")
- if len(host) > 0 {
- parts := strings.Split(host, ",")
- // apache will append the remote address
- host = strings.TrimSpace(parts[len(parts)-1])
- } else {
- host, _, err = net.SplitHostPort(r.RemoteAddr)
- }
+ host, err := GetHost(r)
// determine if we're in Tor
if err != nil {
@@ -93,6 +86,22 @@ func RootHandler(Layout *template.Template, Exits *Exits, domain *gettext.Domain
}
+type IPResp struct {
+ IsTor bool
+}
+
+func APIHandler(Exits *Exits) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ isTor := false
+ if host, err := GetHost(r); err == nil {
+ _, isTor = Exits.IsTor(host)
+ }
+ ip, _ := json.Marshal(IPResp{isTor})
+ w.Write(ip)
+ }
+}
+
func BulkHandler(Layout *template.Template, Exits *Exits, domain *gettext.Domain) http.HandlerFunc {
ApiPath := regexp.MustCompile("^/api/")
diff --git a/utils.go b/utils.go
index 737aa29..88da175 100644
--- a/utils.go
+++ b/utils.go
@@ -8,11 +8,13 @@ import (
"io"
"io/ioutil"
"log"
+ "net"
"net/http"
"net/url"
"os"
"path"
"strconv"
+ "strings"
)
func UpToDate(r *http.Request) bool {
@@ -49,6 +51,19 @@ func GetQS(q url.Values, param string, deflt int) (num int, str string) {
return
}
+func GetHost(r *http.Request) (host string, err error) {
+ // get remote ip
+ host = r.Header.Get("X-Forwarded-For")
+ if len(host) > 0 {
+ parts := strings.Split(host, ",")
+ // apache will append the remote address
+ host = strings.TrimSpace(parts[len(parts)-1])
+ } else {
+ host, _, err = net.SplitHostPort(r.RemoteAddr)
+ }
+ return
+}
+
var HaveManual = map[string]bool{
"ar": true,
"zh_CN": true,
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits