Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api for closing the tunnel. #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,5 @@ call the query API, you can get the below response:
}
]
```

More api descriptions. You can check out [Restful API](docs/restful-API.md)
87 changes: 87 additions & 0 deletions docs/restful-API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Restful API

``quic-tun`` provide some restful API. By these APIs, you can query or operate on the tunnel which are active.
You can set address of the API server listen on by ``--httpd-listen-on`` when you start server/client endpoint server, like below:

```console
./quictun-server --httpd-listen-on 127.0.0.1:18086
```

## Get all tunnels information

**Request order:**

```console
curl --location --request GET http://127.0.0.1:18086/tunnels
```

**For example:**

```console
curl --location --request GET http://127.0.0.1:18086/tunnels
```

**Return to information:**

```console
[
{
"uuid": "9eb73491-ef38-463d-85c3-d4512152d224",
"streamId": 0,
"endpoint": "server",
"serverAppAddr": "172.18.11.2:5915",
"remoteEndpointAddr": "172.18.29.161:56465",
"createdAt": "2022-06-21 11:41:28.85774404 +0800 CST m=+47.535828999",
"serverTotalBytes": 1545,
"clientTotalBytes": 2221,
"serverSendRate": "0.00 kB/s",
"clientSendRate": "0.00 kB/s",
"protocol": "spice",
"protocolProperties": {
"version": "2.2",
"sessionId": "d0306d75",
"channelType": "main",
"serverName": "instance-e548a827-8937-4047-a756-e56937017128",
"serverUUID": "e548a827-8937-4047-a756-e56937017128"
}
},
{
"uuid": "66bad84d-318c-4e14-b3be-a5cb796e7f61",
"streamId": 44,
"endpoint": "server",
"serverAppAddr": "172.18.11.2:5915",
"remoteEndpointAddr": "172.18.29.161:56465",
"createdAt": "2022-06-21 11:41:28.937090895 +0800 CST m=+47.615175866",
"serverTotalBytes": 1545,
"clientTotalBytes": 2221,
"serverSendRate": "0.00 kB/s",
"clientSendRate": "0.00 kB/s",
"protocol": "spice",
"protocolProperties": {
"version": "2.2",
"sessionId": "d0306d75",
"channelType": "record"
}
}
...
]

```

## Close the tunnel according to uuid

**Request order:**

```console
curl --location --request PUT 'http://127.0.0.1:18086/<uuid>/close_tunnel'
```

**For example:**

```console
curl --location --request PUT 'http://127.0.0.1:18086/66bad84d-318c-4e14-b3be-a5cb796e7f61/close_tunnel'
```

**Return to information:**

No return value for successful closure, but the status code is 200
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/cheekybits/genny v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gorilla/mux v1.8.0
github.com/gosuri/uitable v0.0.4
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
Expand Down
30 changes: 28 additions & 2 deletions pkg/restfulapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/kungze/quic-tun/pkg/log"
"github.com/kungze/quic-tun/pkg/tunnel"
)
Expand Down Expand Up @@ -37,9 +39,33 @@ func (h *httpd) getAllStreams(w http.ResponseWriter, request *http.Request) {
}
}

func (h *httpd) closeStream(w http.ResponseWriter, request *http.Request) {
var resp_json []byte
if request.Method != http.MethodPut {
w.WriteHeader(http.StatusMethodNotAllowed)
resp_json, _ = json.Marshal(errorResponse{Msg: "Please use PUT request method"})
} else {
streamUuid, _ := uuid.Parse(mux.Vars(request)["uuid"])
tun, err := tunnel.DataStore.LoadOne(streamUuid)
if err != nil {
w.WriteHeader(http.StatusNotFound)
resp_json, _ = json.Marshal(errorResponse{Msg: "Not found tunnel for uuid"})
} else {
(*tun.Stream).Close()
(*tun.Conn).Close()
}
}
_, err := w.Write(resp_json)
if err != nil {
log.Errorw("Encounter error!", "error", err.Error())
}
}

func (h *httpd) Start() {
http.HandleFunc("/tunnels", h.getAllStreams)
err := http.ListenAndServe(h.ListenAddr, nil)
router := mux.NewRouter()
router.HandleFunc("/tunnels", h.getAllStreams)
router.HandleFunc("/{uuid}/close_tunnel", h.closeStream)
err := http.ListenAndServe(h.ListenAddr, router)
if err != nil {
panic(err)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/tunnel/datastore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package tunnel

import (
"errors"
"sync"

"github.com/google/uuid"
)

type tunnelDataStore struct {
Expand All @@ -17,5 +20,17 @@ func (t *tunnelDataStore) LoadAll() []tunnel {
return tunnels
}

func (t *tunnelDataStore) LoadOne(uuid uuid.UUID) (tunnel, error) {
var tun tunnel
value, ok := t.Load(uuid)
if ok {
tun, ok = value.(tunnel)
if ok {
return tun, nil
}
}
return tun, errors.New("not found tunnel for uuid")
}

// Used to store all active tunnels information
var DataStore = tunnelDataStore{}