Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.

Commit 1a0ac7e

Browse files
authored
Merge pull request #32 from thomotron/spamfix
Authentication Upgrade & Spam Fix
2 parents cbf8cd9 + de96b0e commit 1a0ac7e

File tree

11 files changed

+222
-36
lines changed

11 files changed

+222
-36
lines changed

PhiClient/PhiClient.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ private void ProcessPacket(Packet packet)
108108
this.realmData.PacketToServer += PacketToServerCallback;
109109
this.realmData.Log += Log;
110110

111+
SaveCredentials();
112+
111113
if (OnUsable != null)
112114
{
113115
OnUsable();
@@ -119,6 +121,21 @@ private void ProcessPacket(Packet packet)
119121
}
120122
}
121123

124+
private void SaveCredentials()
125+
{
126+
string key;
127+
if (File.Exists(KEY_FILE))
128+
{
129+
key = File.ReadAllLines(KEY_FILE)[0];
130+
}
131+
else
132+
{
133+
key = GetAuthKey();
134+
}
135+
136+
File.WriteAllLines(KEY_FILE, new string[] { key, currentUser.id.ToString() });
137+
}
138+
122139
private void Log(LogLevel level, string message)
123140
{
124141
if (level == LogLevel.ERROR)
@@ -157,7 +174,8 @@ private void ConnectionCallback()
157174

158175
string nickname = SteamUtility.SteamPersonaName;
159176
string hashedKey = GetHashedAuthKey();
160-
this.SendPacket(new AuthentificationPacket { name = nickname, hashedKey = hashedKey, version = RealmData.VERSION });
177+
int? id = GetId();
178+
this.SendPacket(new AuthentificationPacket { name = nickname, id = id, hashedKey = hashedKey, version = RealmData.VERSION });
161179
Log(LogLevel.INFO, "Trying to authenticate as " + nickname);
162180
}
163181

@@ -195,6 +213,22 @@ private string GetAuthKey()
195213
}
196214
}
197215

216+
private int? GetId()
217+
{
218+
if (File.Exists(KEY_FILE))
219+
{
220+
if (File.ReadAllLines(KEY_FILE).Length > 1)
221+
{
222+
return int.Parse(File.ReadAllLines(KEY_FILE)[1]);
223+
}
224+
else return null;
225+
}
226+
else
227+
{
228+
return null;
229+
}
230+
}
231+
198232
private string GenerateKey(int length)
199233
{
200234
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@@ -260,7 +294,7 @@ public bool SendThings(User user, Dictionary<List<Thing>, int> chosenThings)
260294

261295
realmThings.Add(new KeyValuePair<RealmThing, int>(realmThing, entry.Value));
262296
}
263-
297+
264298
int id = ++this.currentUser.lastTransactionId;
265299
ItemTransaction transaction = new ItemTransaction(id, currentUser, user, chosenThings, realmThings);
266300
realmData.transactions.Add(transaction);

PhiData/Packet.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static Packet Deserialize(byte[] data, RealmData realmData, User user)
4242
public class AuthentificationPacket : Packet
4343
{
4444
public string name;
45+
public int? id;
4546
public string hashedKey;
4647
public string version;
4748

PhiData/RealmData.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace PhiClient
1212
[Serializable]
1313
public class RealmData
1414
{
15-
public const string VERSION = "0.12";
15+
public const string VERSION = "0.14";
1616
public const int CHAT_MESSAGES_TO_SEND = 30;
1717
public const int CHAT_MESSAGE_MAX_LENGTH = 250;
1818

@@ -111,22 +111,19 @@ public void BroadcastPacketExcept(Packet packet, User excludedUser)
111111
}
112112
}
113113

114-
public User ServerAddUser(string name, string hashKey)
114+
public User ServerAddUser(string name, int id)
115115
{
116-
this.lastUserGivenId++;
117-
int id = this.lastUserGivenId;
118116

119117
User user = new User
120118
{
121119
id = id,
122120
name = name,
123121
connected = true,
124-
inGame = false,
125-
hashedKey= hashKey
122+
inGame = false
126123
};
127124

128125
AddUser(user);
129-
EmitLog(LogLevel.INFO, string.Format("Created user {0} ({1})", name, hashKey));
126+
EmitLog(LogLevel.INFO, string.Format("Created user {0} ({1})", name, id));
130127

131128
return user;
132129
}

PhiData/TransactionSystem/AnimalTransaction.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@ public AnimalTransaction(int id, User sender, User receiver, Pawn pawn, RealmAni
2323

2424
public override void OnStartReceiver(RealmData realmData)
2525
{
26+
// Double check to ensure it wasn't bypassed by the sender
27+
if (!receiver.preferences.receiveAnimals)
28+
{
29+
realmData.NotifyPacketToServer(new ConfirmServerTransactionPacket
30+
{
31+
transaction = this,
32+
response = TransactionResponse.DECLINED
33+
});
34+
return;
35+
}
36+
2637
// We ask for confirmation
27-
38+
2839
Dialog_GeneralChoice choiceDialog = new Dialog_GeneralChoice(new DialogChoiceConfig
2940
{
3041
text = sender.name + " wants to send you a " + realmAnimal.FromRealmAnimal(realmData).kindDef.label,
@@ -53,6 +64,17 @@ public override void OnStartReceiver(RealmData realmData)
5364

5465
public override void OnEndReceiver(RealmData realmData)
5566
{
67+
// Double check to ensure it wasn't bypassed by the sender
68+
if (!receiver.preferences.receiveAnimals)
69+
{
70+
realmData.NotifyPacketToServer(new ConfirmServerTransactionPacket
71+
{
72+
transaction = this,
73+
response = TransactionResponse.DECLINED
74+
});
75+
return;
76+
}
77+
5678
// Nothing
5779
if (state == TransactionResponse.ACCEPTED)
5880
{
@@ -78,6 +100,10 @@ public override void OnEndReceiver(RealmData realmData)
78100
{
79101
Messages.Message("Unexpected interruption during item transaction with " + sender.name, MessageTypeDefOf.RejectInput);
80102
}
103+
else if (state == TransactionResponse.TOOFAST)
104+
{
105+
// This should never happen as the server rejects intercepted packets.
106+
}
81107
}
82108

83109
public override void OnEndSender(RealmData realmData)
@@ -95,6 +121,10 @@ public override void OnEndSender(RealmData realmData)
95121
{
96122
Messages.Message("Unexpected interruption during item transaction with " + receiver.name, MessageTypeDefOf.RejectInput);
97123
}
124+
else if (state == TransactionResponse.TOOFAST)
125+
{
126+
Messages.Message("Transaction with " + receiver.name + " was declined by the server. Are you sending animals too quickly?", MessageTypeDefOf.RejectInput);
127+
}
98128
}
99129
}
100130
}

PhiData/TransactionSystem/ColonistTransaction.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@ public ColonistTransaction(int id, User sender, User receiver, Pawn pawn, RealmP
2323

2424
public override void OnStartReceiver(RealmData realmData)
2525
{
26+
// Double check to ensure it wasn't bypassed by the sender
27+
if (!receiver.preferences.receiveColonists)
28+
{
29+
realmData.NotifyPacketToServer(new ConfirmServerTransactionPacket
30+
{
31+
transaction = this,
32+
response = TransactionResponse.DECLINED
33+
});
34+
return;
35+
}
36+
2637
// We ask for confirmation
27-
38+
2839
Dialog_GeneralChoice choiceDialog = new Dialog_GeneralChoice(new DialogChoiceConfig
2940
{
3041
text = sender.name + " wants to send you a colonist",
@@ -53,6 +64,12 @@ public override void OnStartReceiver(RealmData realmData)
5364

5465
public override void OnEndReceiver(RealmData realmData)
5566
{
67+
// Double check to ensure it wasn't bypassed by the sender
68+
if (!receiver.preferences.receiveItems)
69+
{
70+
state = TransactionResponse.DECLINED;
71+
}
72+
5673
// Nothing
5774
if (state == TransactionResponse.ACCEPTED)
5875
{
@@ -78,10 +95,20 @@ public override void OnEndReceiver(RealmData realmData)
7895
{
7996
Messages.Message("Unexpected interruption during item transaction with " + sender.name, MessageTypeDefOf.RejectInput);
8097
}
98+
else if (state == TransactionResponse.TOOFAST)
99+
{
100+
// This should never happen as the server rejects intercepted packets.
101+
}
81102
}
82103

83104
public override void OnEndSender(RealmData realmData)
84105
{
106+
// Double check to ensure it wasn't bypassed by the sender
107+
if (!receiver.preferences.receiveItems)
108+
{
109+
state = TransactionResponse.DECLINED;
110+
}
111+
85112
if (state == TransactionResponse.ACCEPTED)
86113
{
87114
pawn.Destroy();
@@ -95,6 +122,10 @@ public override void OnEndSender(RealmData realmData)
95122
{
96123
Messages.Message("Unexpected interruption during item transaction with " + receiver.name, MessageTypeDefOf.RejectInput);
97124
}
125+
else if (state == TransactionResponse.TOOFAST)
126+
{
127+
Messages.Message("Transaction with " + receiver.name + " was declined by the server. Are you sending colonists too quickly?", MessageTypeDefOf.RejectInput);
128+
}
98129
}
99130
}
100131
}

PhiData/TransactionSystem/ConfirmServerTransactionPacket.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace PhiClient.TransactionSystem
88
{
99
/// <summary>
10-
/// Received by the server
10+
/// Received by the server from the recipient
1111
/// </summary>
1212
[Serializable]
1313
public class ConfirmServerTransactionPacket : Packet
@@ -24,6 +24,7 @@ public override void Apply(User user, RealmData realmData)
2424
{
2525
return;
2626
}
27+
2728
if (transaction.state != TransactionResponse.WAITING)
2829
{
2930
return;

PhiData/TransactionSystem/ItemTransaction.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ public ItemTransaction(int id, User sender, User receiver, Dictionary<List<Thing
2323

2424
public override void OnStartReceiver(RealmData realmData)
2525
{
26+
// Double check to ensure it wasn't bypassed by the sender
27+
if (!receiver.preferences.receiveItems)
28+
{
29+
realmData.NotifyPacketToServer(new ConfirmServerTransactionPacket
30+
{
31+
transaction = this,
32+
response = TransactionResponse.DECLINED
33+
});
34+
return;
35+
}
36+
2637
// We generate a detailed list of what the pack contains
2738
List<KeyValuePair<Thing, int>> things = realmThings.Select((r) => new KeyValuePair<Thing, int>(realmData.FromRealmThing(r.Key), r.Value)).ToList();
2839

@@ -57,6 +68,12 @@ public override void OnStartReceiver(RealmData realmData)
5768

5869
public override void OnEndReceiver(RealmData realmData)
5970
{
71+
// Double check to ensure it wasn't bypassed by the sender
72+
if (!receiver.preferences.receiveItems)
73+
{
74+
state = TransactionResponse.DECLINED;
75+
}
76+
6077
if (state == TransactionResponse.ACCEPTED)
6178
{
6279
// We spawn the new items !
@@ -94,10 +111,20 @@ public override void OnEndReceiver(RealmData realmData)
94111
{
95112
Messages.Message("Unexpected interruption during item transaction with " + sender.name, MessageTypeDefOf.RejectInput);
96113
}
114+
else if (state == TransactionResponse.TOOFAST)
115+
{
116+
// This should never happen as the server rejects intercepted packets
117+
}
97118
}
98119

99120
public override void OnEndSender(RealmData realmData)
100121
{
122+
// Double check to ensure it wasn't bypassed by the sender
123+
if (!receiver.preferences.receiveItems)
124+
{
125+
state = TransactionResponse.DECLINED;
126+
}
127+
101128
if (state == TransactionResponse.ACCEPTED)
102129
{
103130
foreach (KeyValuePair<List<Thing>, int> entry in things)
@@ -143,6 +170,10 @@ public override void OnEndSender(RealmData realmData)
143170
{
144171
Messages.Message("Unexpected interruption during item transaction with " + receiver.name, MessageTypeDefOf.RejectInput);
145172
}
173+
else if (state == TransactionResponse.TOOFAST)
174+
{
175+
Messages.Message("Transaction with " + receiver.name + " was declined by the server. Are you sending items too quickly?", MessageTypeDefOf.RejectInput);
176+
}
146177
}
147178
}
148179
}

PhiData/TransactionSystem/StartTransactionPacket.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public override void Apply(User user, RealmData realmData)
1717
{
1818
realmData.transactions.Add(transaction);
1919
user.lastTransactionId = transaction.id;
20+
user.lastTransactionTime = DateTime.Now;
2021

2122
realmData.NotifyPacket(transaction.receiver, new ReceiveTransactionPacket { transaction = transaction });
2223
}

PhiData/TransactionSystem/Transaction.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public bool IsFinished()
4242
{
4343
return state == TransactionResponse.ACCEPTED
4444
|| state == TransactionResponse.DECLINED
45-
|| state == TransactionResponse.INTERRUPTED;
45+
|| state == TransactionResponse.INTERRUPTED
46+
|| state == TransactionResponse.TOOFAST;
4647
}
4748

4849
[OnSerializing]
@@ -71,6 +72,7 @@ public enum TransactionResponse
7172
WAITING,
7273
ACCEPTED,
7374
DECLINED,
74-
INTERRUPTED
75+
INTERRUPTED,
76+
TOOFAST
7577
}
7678
}

PhiData/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class User : IDable
1313

1414
public int id;
1515
public string name;
16-
public string hashedKey;
1716
public bool connected;
1817
public bool inGame;
1918
public UserPreferences preferences = new UserPreferences();
2019

2120
public int lastTransactionId = 0;
21+
public DateTime lastTransactionTime = DateTime.MinValue;
2222

2323
public int getID()
2424
{

0 commit comments

Comments
 (0)