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 endpoint to disconnect peers #23

Merged
merged 4 commits into from
Nov 16, 2018
Merged
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
26 changes: 26 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func (d *Daemon) handleConn(c net.Conn) {
return
}

case pb.Request_DISCONNECT:
res := d.doDisconnect(&req)
err := w.WriteMsg(res)
if err != nil {
log.Debugf("Error writing response: %s", err.Error())
return
}

default:
log.Debugf("Unexpected request type: %d", *req.Type)
return
Expand Down Expand Up @@ -179,6 +187,24 @@ func (d *Daemon) doConnect(req *pb.Request) *pb.Response {
return okResponse()
}

func (d *Daemon) doDisconnect(req *pb.Request) *pb.Response {
if req.Disconnect == nil {
return errorResponseString("Malformed request; missing parameters")
}

p, err := peer.IDFromBytes(req.Disconnect.GetPeer())
if err != nil {
return errorResponse(err)
}

err = d.host.Network().ClosePeer(p)
if err != nil {
return errorResponse(err)
}

return okResponse()
}

func (d *Daemon) doStreamOpen(req *pb.Request) (*pb.Response, inet.Stream) {
ctx, cancel := context.WithTimeout(d.ctx, DefaultTimeout)
defer cancel()
Expand Down
Loading