-
-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from lvht/dtls
WIP: DTLS
- Loading branch information
Showing
9 changed files
with
212 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,59 @@ | ||
package handler | ||
|
||
// 暂时没有实现 | ||
import ( | ||
"crypto/tls" | ||
"encoding/hex" | ||
"log" | ||
"net" | ||
"time" | ||
"os" | ||
|
||
"github.com/bjdgyc/anylink/sessdata" | ||
"github.com/pion/dtls/v2" | ||
"github.com/pion/dtls/v2/pkg/crypto/selfsign" | ||
"github.com/pion/logging" | ||
) | ||
|
||
func startDtls() { | ||
certificate, err := selfsign.GenerateSelfSigned() | ||
|
||
logf := logging.NewDefaultLoggerFactory() | ||
logf.DefaultLogLevel = logging.LogLevelTrace | ||
f, err := os.OpenFile("/tmp/key.log", os.O_TRUNC|os.O_RDWR, 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
config := &dtls.Config{ | ||
Certificates: []tls.Certificate{certificate}, | ||
InsecureSkipVerify: true, | ||
ExtendedMasterSecret: dtls.DisableExtendedMasterSecret, | ||
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, | ||
LoggerFactory: logf, | ||
KeyLogWriter: f, | ||
} | ||
|
||
addr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 4433} | ||
|
||
ln, err := dtls.Listen("udp", addr, config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
c, err := ln.Accept() | ||
if err != nil { | ||
log.Println("Accept error", err) | ||
continue | ||
} | ||
|
||
go func() { | ||
time.Sleep(1 * time.Second) | ||
cc := c.(*dtls.Conn) | ||
id := hex.EncodeToString(cc.ConnectionState().SessionID) | ||
s, ok := ss.Load(id) | ||
log.Println("get link", id, ok) | ||
cs := s.(*sessdata.ConnSession) | ||
LinkDtls(c, cs) | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package handler | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/bjdgyc/anylink/base" | ||
"github.com/bjdgyc/anylink/sessdata" | ||
) | ||
|
||
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { | ||
defer func() { | ||
base.Debug("LinkDtls return", cSess.IpAddr) | ||
_ = conn.Close() | ||
cSess.Close() | ||
}() | ||
|
||
var ( | ||
dead = time.Duration(cSess.CstpDpd+5) * time.Second | ||
) | ||
|
||
go dtlsWrite(conn, cSess) | ||
|
||
for { | ||
err := conn.SetReadDeadline(time.Now().Add(dead)) | ||
if err != nil { | ||
base.Error("SetDeadline: ", err) | ||
return | ||
} | ||
hdata := make([]byte, BufferSize) | ||
n, err := conn.Read(hdata) | ||
if err != nil { | ||
base.Error("read hdata: ", err) | ||
return | ||
} | ||
|
||
// 限流设置 | ||
err = cSess.RateLimit(n, true) | ||
if err != nil { | ||
base.Error(err) | ||
} | ||
|
||
switch hdata[0] { | ||
case 0x07: // KEEPALIVE | ||
// do nothing | ||
base.Debug("recv keepalive", cSess.IpAddr) | ||
case 0x05: // DISCONNECT | ||
base.Debug("DISCONNECT", cSess.IpAddr) | ||
return | ||
case 0x03: // DPD-REQ | ||
base.Debug("recv DPD-REQ", cSess.IpAddr) | ||
if payloadOut(cSess, sessdata.LTypeIPData, 0x04, nil) { | ||
return | ||
} | ||
case 0x04: | ||
base.Debug("recv DPD-RESP", cSess.IpAddr) | ||
case 0x00: // DATA | ||
if payloadIn(cSess, sessdata.LTypeIPData, 0x00, hdata[1:]) { | ||
return | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
func dtlsWrite(conn net.Conn, cSess *sessdata.ConnSession) { | ||
defer func() { | ||
base.Debug("dtlsWrite return", cSess.IpAddr) | ||
_ = conn.Close() | ||
cSess.Close() | ||
}() | ||
|
||
var ( | ||
header []byte | ||
payload *sessdata.Payload | ||
) | ||
|
||
for { | ||
select { | ||
case payload = <-cSess.PayloadOut: | ||
case <-cSess.CloseChan: | ||
return | ||
} | ||
|
||
if payload.LType != sessdata.LTypeIPData { | ||
continue | ||
} | ||
|
||
header = []byte{payload.PType} | ||
header = append(header, payload.Data...) | ||
n, err := conn.Write(header) | ||
if err != nil { | ||
base.Error("write err", err) | ||
return | ||
} | ||
|
||
// 限流设置 | ||
err = cSess.RateLimit(n, false) | ||
if err != nil { | ||
base.Error(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.