1
1
use base16:: encode_lower;
2
2
use crypto:: { digest:: Digest , sha2:: Sha256 } ;
3
3
use hex:: decode;
4
+ use machine_uuid;
4
5
use reqwest:: blocking:: Client ;
6
+ use serde:: Deserialize ;
7
+ use serde_json;
5
8
use uuid:: Uuid ;
6
- use machine_uuid;
7
9
8
10
use aes:: Aes256 ;
9
11
use block_modes:: block_padding:: Pkcs7 ;
10
12
use block_modes:: { BlockMode , Cbc } ;
11
13
12
- const BASE_URL : & str = "https://keyauth.com/api/" ;
14
+ const BASE_URL : & str = "https://keyauth.com/api/v2/ " ;
13
15
14
16
type Aes256Cbc = Cbc < Aes256 , Pkcs7 > ;
17
+
15
18
pub struct KeyauthApi {
16
19
name : String ,
17
20
owner_id : String ,
18
21
secret : String ,
22
+ key : Option < String > ,
23
+ init_iv : String ,
24
+ }
25
+
26
+ #[ derive( Deserialize , Debug ) ]
27
+ pub struct Key {
28
+ key : String ,
29
+ expiry : String ,
30
+ level : u32 ,
19
31
}
20
32
21
33
impl KeyauthApi {
@@ -24,68 +36,92 @@ impl KeyauthApi {
24
36
name : name,
25
37
owner_id : owner_id,
26
38
secret : secret,
39
+ key : None ,
40
+ init_iv : String :: new ( ) ,
27
41
}
28
42
}
29
43
30
- pub fn init ( & self ) -> Result < ( ) , String > {
44
+ pub fn init ( & mut self ) -> Result < ( ) , String > {
31
45
let session_iv = Uuid :: new_v4 ( ) . to_simple ( ) . to_string ( ) [ ..8 ] . to_string ( ) ;
32
46
let mut hasher = Sha256 :: new ( ) ;
33
47
hasher. input ( session_iv. as_bytes ( ) ) ;
34
48
let init_iv: String = hasher. result_str ( ) ;
49
+ self . init_iv = init_iv;
35
50
let data = format ! (
36
51
"type={}&name={}&ownerid={}&init_iv={}" ,
37
52
encode_lower( b"init" ) ,
38
53
encode_lower( self . name. as_bytes( ) ) ,
39
54
encode_lower( self . owner_id. as_bytes( ) ) ,
40
- & init_iv
55
+ & self . init_iv
41
56
) ;
57
+
42
58
43
59
let req = Self :: make_req ( data) ;
44
60
45
- let response = Encryption :: decrypt ( req. text ( ) . unwrap ( ) , & self . secret , & init_iv) ;
61
+ let response = Encryption :: decrypt ( req. text ( ) . unwrap ( ) , & self . secret , & self . init_iv ) ;
46
62
47
- if response == "KeyAuth_Disabled" . to_string ( ) {
48
- Err ( "The program key you tried to use doesn't exist" . to_string ( ) )
49
- } else if response == "KeyAuth_Initialized" . to_string ( ) {
63
+ let json_rep: serde_json:: Value = serde_json:: from_str ( & response) . unwrap ( ) ;
64
+ if json_rep[ "success" ] . as_bool ( ) . unwrap ( ) {
50
65
Ok ( ( ) )
51
66
} else {
52
- Err ( "The program key you tried to use doesn't exist" . to_string ( ) )
67
+ Err ( json_rep [ "message" ] . as_str ( ) . unwrap ( ) . to_string ( ) )
53
68
}
54
69
}
55
70
56
- pub fn login ( & self , key : String , hwid : Option < String > ) -> Result < ( ) , String > {
71
+ pub fn login ( & mut self , key : String , hwid : Option < String > ) -> Result < Key , String > {
57
72
let hwid = match hwid {
58
73
Some ( hwid) => hwid,
59
74
None => Self :: get_hwid ( ) ,
60
75
} ;
61
- let session_iv = Uuid :: new_v4 ( ) . to_simple ( ) . to_string ( ) [ ..8 ] . to_string ( ) ;
62
- let mut hasher = Sha256 :: new ( ) ;
63
- hasher. input ( session_iv. as_bytes ( ) ) ;
64
- let init_iv: String = hasher. result_str ( ) ;
76
+ if self . init_iv == String :: new ( ) {
77
+ return Err ( "NotInitalized" . to_string ( ) ) ;
78
+ }
65
79
66
80
let data = format ! (
67
81
"type={}&key={}&hwid={}&name={}&ownerid={}&init_iv={}" ,
68
82
encode_lower( b"login" ) ,
69
- Encryption :: encrypt( key, & self . secret, & init_iv) ,
70
- Encryption :: encrypt( hwid, & self . secret, & init_iv) ,
83
+ Encryption :: encrypt( & key, & self . secret, & self . init_iv) ,
84
+ Encryption :: encrypt( & hwid, & self . secret, & self . init_iv) ,
71
85
encode_lower( self . name. as_bytes( ) ) ,
72
86
encode_lower( self . owner_id. as_bytes( ) ) ,
73
- & init_iv
87
+ & self . init_iv
74
88
) ;
75
89
76
90
let req = Self :: make_req ( data) ;
77
91
78
- let response = Encryption :: decrypt ( req. text ( ) . unwrap ( ) , & self . secret , & init_iv) ;
79
- if response == "KeyAuth_Valid" . to_string ( ) {
92
+ let response = Encryption :: decrypt ( req. text ( ) . unwrap ( ) , & self . secret , & self . init_iv ) ;
93
+ let json_rep: serde_json:: Value = serde_json:: from_str ( & response) . unwrap ( ) ;
94
+ if json_rep[ "success" ] . as_bool ( ) . unwrap ( ) {
95
+ self . key = Some ( key) ;
96
+ Ok ( serde_json:: from_value ( json_rep[ "info" ] . clone ( ) ) . unwrap ( ) )
97
+ } else {
98
+ Err ( json_rep[ "message" ] . as_str ( ) . unwrap ( ) . to_string ( ) )
99
+ }
100
+ }
101
+
102
+ pub fn log ( & self , msg : String ) -> Result < ( ) , String > {
103
+ if msg. len ( ) > 128 {
104
+ return Err ( "MaxLogSize128" . to_string ( ) ) ;
105
+ }
106
+ match self . key {
107
+ Some ( _) => { }
108
+ None => return Err ( "NotLoggedIn" . to_string ( ) ) ,
109
+ } ;
110
+ let key = self . key . clone ( ) . unwrap ( ) ;
111
+ let data = format ! (
112
+ "type={}&key={}&message={}&name={}&ownerid={}&init_iv={}" ,
113
+ encode_lower( b"log" ) ,
114
+ Encryption :: encrypt( & key, & self . secret, & self . init_iv) ,
115
+ Encryption :: encrypt( & msg, & self . secret, & self . init_iv) ,
116
+ encode_lower( self . name. as_bytes( ) ) ,
117
+ encode_lower( self . owner_id. as_bytes( ) ) ,
118
+ & self . init_iv,
119
+ ) ;
120
+ let resp = Self :: make_req ( data) ;
121
+ if resp. status ( ) . is_success ( ) {
80
122
Ok ( ( ) )
81
- } else if response == "KeyAuth_Invalid" . to_string ( ) {
82
- Err ( "Key not found" . to_string ( ) )
83
- } else if response == "KeyAuth_InvalidHWID" . to_string ( ) {
84
- Err ( "This computer doesn't match the computer the key is locked to. If you reset your computer, contact the application owner" . to_string ( ) )
85
- } else if response == "KeyAuth_Expired" . to_string ( ) {
86
- Err ( "This key is expired" . to_string ( ) )
87
123
} else {
88
- Err ( "Application Failed To Connect. Try again or contact application owner" . to_string ( ) )
124
+ Err ( resp . status ( ) . as_str ( ) . to_string ( ) )
89
125
}
90
126
}
91
127
@@ -104,7 +140,7 @@ impl KeyauthApi {
104
140
if cfg ! ( windows) {
105
141
machine_uuid:: get ( )
106
142
} else {
107
- "None" . into ( )
143
+ "None" . to_string ( )
108
144
}
109
145
}
110
146
}
@@ -127,7 +163,7 @@ impl Encryption {
127
163
cipher. decrypt_vec ( & mut buf) . unwrap ( )
128
164
}
129
165
130
- fn encrypt ( message : String , enc_key : & String , iv : & String ) -> String {
166
+ fn encrypt ( message : & String , enc_key : & String , iv : & String ) -> String {
131
167
let mut hasher = Sha256 :: new ( ) ;
132
168
hasher. input ( enc_key. as_bytes ( ) ) ;
133
169
let _key: String = hasher. result_str ( ) [ ..32 ] . to_owned ( ) ;
0 commit comments