GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Change API endpoint to GET /bridge-state.

We are GETting rather than POSTing something, and "bridge-state" is a
more descriptive resource name.

This patch also merges our Web interface and our API handlers, making
the code simpler.
parent 29084908
......@@ -16,27 +16,29 @@ Then, run the binary:
bridgestrap
By default, bridgestrap will listen on port 5000. To use its Web interface,
point your browser to the address and port that bridgestrap is listening on.
Use the argument `-addr` to listen to a custom address and port.
By default, bridgestrap will listen on port 5000. To use its Web interface
(don't forget to turn it on by using the `-web` switch_), point your browser to
the address and port that bridgestrap is listening on. Use the argument
`-addr` to listen to a custom address and port.
Input
-----
Clients send bridge lines to a REST API, using an HTTP POST request:
Clients send bridge lines (in this case `STRING`) to a REST API, using an HTTP
GET request:
{"bridge_line": "STRING"}
https://HOST/bridge-state?bridge_line=STRING
The value of `bridge_line` can be any bridge line (excluding the "Bridge"
prefix) that tor accepts. Here are a few examples:
The value of `bridge_line` can be any URL-encoded bridge line (excluding the
"Bridge" prefix) that tor accepts. Here are a few examples:
* `{"bridge_line": "1.2.3.4:1234"}`
* `{"bridge_line": "1.2.3.4:1234 1234567890ABCDEF1234567890ABCDEF12345678"}`
* `{"bridge_line": "obfs4 1.2.3.4:1234 cert=fJRlJc0T7i2Qkw3SyLQq+M6iTGs9ghLHK65LBy/MQewXJpNOKFq63Om1JHVkLlrmEBbX1w iat-mode=0"}`
* `1.2.3.4%3A1234`
* `1.2.3.4%3A1234%201234567890ABCDEF1234567890ABCDEF12345678`
* `obfs4%201.2.3.4%3A1234%20cert%3DfJRlJc0T7i2Qkw3SyLQq%2BM6iTGs9ghLHK65LBy%2FMQewXJpNOKFq63Om1JHVkLlrmEBbX1w%20iat-mode%3D0`
You can test bridgestrap's API as follows:
curl -X POST localhost:5000/api/test -d '{"bridge_line": "1.2.3.4:1234"}'
curl "localhost:5000/bridge-state?bridge_line=1.2.3.4%3A1234"
Output
------
......
......@@ -15,11 +15,6 @@ var IndexPage string
var SuccessPage string
var FailurePage string
// TestResult represents an incoming JSON request.
type TestRequest struct {
BridgeLine string `json:"bridge_line"`
}
// TestResult represents the result of a test, sent back to the client as JSON
// object.
type TestResult struct {
......@@ -93,46 +88,38 @@ func createJsonResult(err error, start time.Time) string {
return string(jsonResult)
}
func APITestBridge(w http.ResponseWriter, r *http.Request) {
func BridgeState(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var req TestRequest
var err error
decoder := json.NewDecoder(r.Body)
if err = decoder.Decode(&req); err != nil {
SendJSONResponse(w, createJsonResult(fmt.Errorf("Invalid JSON request."), start))
return
}
if req.BridgeLine == "" {
SendJSONResponse(w, createJsonResult(fmt.Errorf("No bridge line given."), start))
return
}
err = bootstrapTorOverBridge(req.BridgeLine)
SendJSONResponse(w, createJsonResult(err, start))
}
func TestBridge(w http.ResponseWriter, r *http.Request) {
// Rate-limit requests to prevent someone from abusing this service as a
// port scanner.
if limiter.Allow() == false {
SendHtmlResponse(w, "Rate limit exceeded.")
return
r.ParseForm()
isWebRequest := r.Form.Get("web_request") != ""
if isWebRequest {
// Rate-limit Web requests to prevent someone from abusing this service
// as a port scanner.
if limiter.Allow() == false {
SendHtmlResponse(w, "Rate limit exceeded.")
return
}
}
r.ParseForm()
bridgeLine := r.Form.Get("bridge_line")
if bridgeLine == "" {
SendHtmlResponse(w, "No bridge line given.")
if isWebRequest {
SendHtmlResponse(w, "No bridge line given.")
} else {
SendJSONResponse(w, createJsonResult(fmt.Errorf("No bridge line given"), start))
}
return
}
if err := bootstrapTorOverBridge(bridgeLine); err == nil {
SendHtmlResponse(w, SuccessPage)
err := bootstrapTorOverBridge(bridgeLine)
if isWebRequest {
if err := bootstrapTorOverBridge(bridgeLine); err == nil {
SendHtmlResponse(w, SuccessPage)
} else {
SendHtmlResponse(w, FailurePage)
}
} else {
SendHtmlResponse(w, FailurePage)
SendJSONResponse(w, createJsonResult(err, start))
}
}
......@@ -25,10 +25,10 @@ type Routes []Route
var routes = Routes{
Route{
"APITestBridge",
"POST",
"/api/bridge",
APITestBridge,
"BridgeState",
"GET",
"/bridge-state",
BridgeState,
},
}
......@@ -96,13 +96,6 @@ func main() {
if web {
log.Println("Enabling web interface.")
routes = append(routes,
Route{
"TestBridge",
"POST",
"/bridge",
TestBridge,
})
routes = append(routes,
Route{
"Index",
......
......@@ -16,7 +16,7 @@
</header>
<section id="content">
<form method="POST" action="bridge">
<form method="GET" action="bridge-state">
<h1>Test your Tor bridge</h1>
<p>Are you wondering if your new Tor bridge works? You have come to the
right place! This service lets you test any kind of bridge&mdash;be it
......@@ -33,6 +33,7 @@
<li><tt style="font-size: 0.8rem">obfs4 1.2.3.4:4321 cert=aY09OloaS1d3eUVfc/9ZAJfgV73wiSx6kuY5bxhwtq4MYkUpt26wg3hLGY0dhPvQuA/xAQ iat-mode=0</tt></li>
</ul>
<input type="hidden" name="web_request" value="1">
<input type="text" required name="bridge_line" size="50" placeholder="obfs4 1.2.3.4:4321 cert=aY09OloaS1d3eUVfc/9ZAJfgV73wiSx6kuY5bxhwtq4MYkUpt26wg3hLGY0dhPvQuA/xAQ iat-mode=0">
<label></label>
<button type="submit">Test</button>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment