| 
									
										
										
										
											2016-02-03 11:58:42 +01:00
										 |  |  | package protocol | 
					
						
							| 
									
										
										
										
											2015-09-05 17:48:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-02-25 21:50:10 +01:00
										 |  |  | 	"crypto/hmac" | 
					
						
							| 
									
										
										
										
											2015-09-07 23:48:37 +02:00
										 |  |  | 	"crypto/md5" | 
					
						
							| 
									
										
										
										
											2016-02-25 21:50:10 +01:00
										 |  |  | 	"hash" | 
					
						
							| 
									
										
										
										
											2015-09-11 17:27:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-20 20:55:45 +02:00
										 |  |  | 	"v2ray.com/core/common/uuid" | 
					
						
							| 
									
										
										
										
											2015-09-05 17:48:38 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 18:19:17 +02:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	IDBytesLen = 16 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 21:50:10 +01:00
										 |  |  | type IDHash func(key []byte) hash.Hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func DefaultIDHash(key []byte) hash.Hash { | 
					
						
							|  |  |  | 	return hmac.New(md5.New, key) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 12:00:46 +02:00
										 |  |  | // The ID of en entity, in the form of an UUID. | 
					
						
							| 
									
										
										
										
											2015-09-14 18:19:17 +02:00
										 |  |  | type ID struct { | 
					
						
							| 
									
										
										
										
											2015-12-12 21:40:16 +01:00
										 |  |  | 	uuid   *uuid.UUID | 
					
						
							| 
									
										
										
										
											2015-09-26 22:32:45 +02:00
										 |  |  | 	cmdKey [IDBytesLen]byte | 
					
						
							| 
									
										
										
										
											2015-09-14 18:19:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-07 13:13:48 +01:00
										 |  |  | // Equals returns true if this ID equals to the other one. | 
					
						
							| 
									
										
										
										
											2016-11-27 21:39:09 +01:00
										 |  |  | func (v *ID) Equals(another *ID) bool { | 
					
						
							|  |  |  | 	return v.uuid.Equals(another.uuid) | 
					
						
							| 
									
										
										
										
											2016-01-20 17:31:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-27 21:39:09 +01:00
										 |  |  | func (v *ID) Bytes() []byte { | 
					
						
							|  |  |  | 	return v.uuid.Bytes() | 
					
						
							| 
									
										
										
										
											2015-12-12 21:40:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-27 21:39:09 +01:00
										 |  |  | func (v *ID) String() string { | 
					
						
							|  |  |  | 	return v.uuid.String() | 
					
						
							| 
									
										
										
										
											2015-12-12 21:40:16 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-16 00:06:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-27 21:39:09 +01:00
										 |  |  | func (v *ID) UUID() *uuid.UUID { | 
					
						
							|  |  |  | 	return v.uuid | 
					
						
							| 
									
										
										
										
											2016-01-09 00:10:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 13:39:02 +01:00
										 |  |  | func (v ID) CmdKey() []byte { | 
					
						
							|  |  |  | 	return v.cmdKey[:] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-12 21:40:16 +01:00
										 |  |  | func NewID(uuid *uuid.UUID) *ID { | 
					
						
							| 
									
										
										
										
											2016-01-12 13:39:02 +01:00
										 |  |  | 	id := &ID{uuid: uuid} | 
					
						
							| 
									
										
										
										
											2015-09-16 00:06:22 +02:00
										 |  |  | 	md5hash := md5.New() | 
					
						
							| 
									
										
										
										
											2015-12-12 21:40:16 +01:00
										 |  |  | 	md5hash.Write(uuid.Bytes()) | 
					
						
							| 
									
										
										
										
											2015-09-14 21:59:44 +02:00
										 |  |  | 	md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")) | 
					
						
							| 
									
										
										
										
											2016-01-20 16:45:50 +00:00
										 |  |  | 	md5hash.Sum(id.cmdKey[:0]) | 
					
						
							| 
									
										
										
										
											2016-01-12 13:39:02 +01:00
										 |  |  | 	return id | 
					
						
							| 
									
										
										
										
											2015-09-07 23:48:19 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-07 21:07:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID { | 
					
						
							|  |  |  | 	alterIDs := make([]*ID, alterIDCount) | 
					
						
							|  |  |  | 	prevID := primary.UUID() | 
					
						
							|  |  |  | 	for idx := range alterIDs { | 
					
						
							|  |  |  | 		newid := prevID.Next() | 
					
						
							|  |  |  | 		// TODO: check duplicates | 
					
						
							|  |  |  | 		alterIDs[idx] = NewID(newid) | 
					
						
							|  |  |  | 		prevID = newid | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return alterIDs | 
					
						
							|  |  |  | } |