forked from bronze1man/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msChapv2.go
67 lines (59 loc) · 1.58 KB
/
msChapv2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package radius
import (
"encoding/binary"
"fmt"
"strconv"
)
type MsChapV2Packet struct {
Eap *EapPacket //解密的时候的eap信息,不使用里面的data
OpCode MsChapV2OpCode
Data []byte
}
func (p *MsChapV2Packet) ToEap() *EapPacket {
eap := p.Eap.Copy()
eap.Data = make([]byte, len(p.Data)+4)
eap.Data[0] = byte(p.OpCode)
eap.Data[1] = byte(eap.Identifier)
binary.BigEndian.PutUint16(eap.Data[2:4], uint16(len(p.Data)+4))
copy(eap.Data[4:], p.Data)
return eap
}
func MsChapV2PacketFromEap(eap *EapPacket) (p *MsChapV2Packet, err error) {
p = &MsChapV2Packet{
Eap: eap,
}
if len(eap.Data) < 4 {
return nil, fmt.Errorf("[MsChapV2PacketFromEap] protocol error 1, packet too small")
}
p.OpCode = MsChapV2OpCode(eap.Data[0])
p.Data = append([]byte(nil), eap.Data[4:]...)
return p, nil
}
//不包括eap的信息
func (p *MsChapV2Packet) String() string {
return fmt.Sprintf("OpCode:%s Data:[%#v]", p.OpCode, p.Data)
}
type MsChapV2OpCode uint8
const (
MsChapV2OpCodeChallenge MsChapV2OpCode = 1
MsChapV2OpCodeResponse MsChapV2OpCode = 2
MsChapV2OpCodeSuccess MsChapV2OpCode = 3
MsChapV2OpCodeFailure MsChapV2OpCode = 4
MsChapV2OpCodeChangePassword MsChapV2OpCode = 7
)
func (c MsChapV2OpCode) String() string {
switch c {
case MsChapV2OpCodeChallenge:
return "Challenge"
case MsChapV2OpCodeResponse:
return "Response"
case MsChapV2OpCodeSuccess:
return "Success"
case MsChapV2OpCodeFailure:
return "Failure"
case MsChapV2OpCodeChangePassword:
return "ChangePassword"
default:
return "unknow MsChapV2OpCode " + strconv.Itoa(int(c))
}
}