util.go 2.54 KB
Newer Older
1
2
3
4
package util

import (
	"encoding/json"
5
	"errors"
6
	"net"
7

Cecylia Bocovich's avatar
Cecylia Bocovich committed
8
9
	"github.com/pion/ice/v2"
	"github.com/pion/sdp/v3"
10
	"github.com/pion/webrtc/v3"
11
12
)

13
func SerializeSessionDescription(desc *webrtc.SessionDescription) (string, error) {
14
	bytes, err := json.Marshal(*desc)
15
16
	if err != nil {
		return "", err
17
	}
18
	return string(bytes), nil
19
20
}

21
func DeserializeSessionDescription(msg string) (*webrtc.SessionDescription, error) {
22
23
	var parsed map[string]interface{}
	err := json.Unmarshal([]byte(msg), &parsed)
24
25
	if err != nil {
		return nil, err
26
27
	}
	if _, ok := parsed["type"]; !ok {
28
		return nil, errors.New("cannot deserialize SessionDescription without type field")
29
30
	}
	if _, ok := parsed["sdp"]; !ok {
31
		return nil, errors.New("cannot deserialize SessionDescription without sdp field")
32
33
34
35
36
	}

	var stype webrtc.SDPType
	switch parsed["type"].(string) {
	default:
37
		return nil, errors.New("Unknown SDP type")
38
39
40
41
42
43
44
45
46
47
48
49
50
	case "offer":
		stype = webrtc.SDPTypeOffer
	case "pranswer":
		stype = webrtc.SDPTypePranswer
	case "answer":
		stype = webrtc.SDPTypeAnswer
	case "rollback":
		stype = webrtc.SDPTypeRollback
	}

	return &webrtc.SessionDescription{
		Type: stype,
		SDP:  parsed["sdp"].(string),
51
	}, nil
52
}
53
54
55
56
57
58
59

// Stolen from https://github.com/golang/go/pull/30278
func IsLocal(ip net.IP) bool {
	if ip4 := ip.To4(); ip4 != nil {
		// Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918
		return ip4[0] == 10 ||
			(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
Peter Gerber's avatar
Peter Gerber committed
60
61
62
63
64
			(ip4[0] == 192 && ip4[1] == 168) ||
			// Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598
			(ip4[0] == 100 && ip4[1]&0xc0 == 64) ||
			// Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927
			(ip4[0] == 169 && ip4[1] == 254)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	}
	// Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193
	return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
}

// Removes local LAN address ICE candidates
func StripLocalAddresses(str string) string {
	var desc sdp.SessionDescription
	err := desc.Unmarshal([]byte(str))
	if err != nil {
		return str
	}
	for _, m := range desc.MediaDescriptions {
		attrs := make([]sdp.Attribute, 0)
		for _, a := range m.Attributes {
			if a.IsICECandidate() {
Cecylia Bocovich's avatar
Cecylia Bocovich committed
81
82
83
				c, err := ice.UnmarshalCandidate(a.Value)
				if err == nil && c.Type() == ice.CandidateTypeHost {
					ip := net.ParseIP(c.Address())
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
					if ip != nil && (IsLocal(ip) || ip.IsUnspecified() || ip.IsLoopback()) {
						/* no append in this case */
						continue
					}
				}
			}
			attrs = append(attrs, a)
		}
		m.Attributes = attrs
	}
	bts, err := desc.Marshal()
	if err != nil {
		return str
	}
	return string(bts)
}