Advertisement
Guest User

Message all steam friends - Steam Authentication Optional

a guest
Aug 31st, 2016
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.59 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Threading;
  5. using SteamKit2;
  6.  
  7. namespace Friends_Auth
  8. {
  9. class Program
  10. {
  11. static SteamClient steamClient;
  12. static CallbackManager manager;
  13.  
  14. static SteamUser steamUser;
  15. static SteamFriends steamFriends;
  16.  
  17. static bool isRunning, steamAuth;
  18.  
  19. static string user, pass, message;
  20. static string authCode, twoFactorAuth;
  21.  
  22. static void Main(string[] args)
  23. {
  24. if (args.Length < 2)
  25. {
  26. Console.WriteLine("Sample4: No username and password specified!");
  27. return;
  28. }
  29.  
  30. // save our logon details
  31. user = args[0];
  32. pass = args[1];
  33. message = args[2];
  34. steamAuth = Boolean.Parse(args[3]);
  35.  
  36. // create our steamclient instance
  37. steamClient = new SteamClient(System.Net.Sockets.ProtocolType.Tcp);
  38. // create the callback manager which will route callbacks to function calls
  39. manager = new CallbackManager(steamClient);
  40.  
  41. // get the steamuser handler, which is used for logging on after successfully connecting
  42. steamUser = steamClient.GetHandler<SteamUser>();
  43. // get the steam friends handler, which is used for interacting with friends on the network after logging on
  44. steamFriends = steamClient.GetHandler<SteamFriends>();
  45.  
  46. // register a few callbacks we're interested in
  47. // these are registered upon creation to a callback manager, which will then route the callbacks
  48. // to the functions specified
  49. manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
  50. manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
  51.  
  52. manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
  53. manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
  54.  
  55. if(steamAuth)
  56. // this callback is triggered when the steam servers wish for the client to store the sentry file
  57. manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
  58.  
  59. // we use the following callbacks for friends related activities
  60. manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
  61. manager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
  62. manager.Subscribe<SteamFriends.PersonaStateCallback>(OnPersonaState);
  63. manager.Subscribe<SteamFriends.FriendAddedCallback>(OnFriendAdded);
  64.  
  65. isRunning = true;
  66.  
  67. Console.WriteLine("Connecting to Steam...");
  68.  
  69. // initiate the connection
  70. steamClient.Connect();
  71. // create our callback handling loop
  72. while (isRunning)
  73. {
  74. // in order for the callbacks to get routed, they need to be handled by the manager
  75. manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  76.  
  77. }
  78. }
  79.  
  80. static void OnConnected(SteamClient.ConnectedCallback callback)
  81. {
  82. if (callback.Result != EResult.OK)
  83. {
  84. Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
  85.  
  86. isRunning = false;
  87. return;
  88. }
  89.  
  90. Console.WriteLine("Connected to Steam! Logging in '{0}'...", user);
  91.  
  92. if(steamAuth)
  93. {
  94. byte[] sentryHash = null;
  95. if (File.Exists("sentry.bin"))
  96. {
  97. // if we have a saved sentry file, read and sha-1 hash it
  98. byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  99. sentryHash = CryptoHelper.SHAHash(sentryFile);
  100. }
  101.  
  102. steamUser.LogOn(new SteamUser.LogOnDetails
  103. {
  104. Username = user,
  105. Password = pass,
  106.  
  107. // in this sample, we pass in an additional authcode
  108. // this value will be null (which is the default) for our first logon attempt
  109. AuthCode = authCode,
  110.  
  111. // if the account is using 2-factor auth, we'll provide the two factor code instead
  112. // this will also be null on our first logon attempt
  113. TwoFactorCode = twoFactorAuth,
  114.  
  115. // our subsequent logons use the hash of the sentry file as proof of ownership of the file
  116. // this will also be null for our first (no authcode) and second (authcode only) logon attempts
  117. SentryFileHash = sentryHash,
  118. });
  119. }
  120. else
  121. {
  122. steamUser.LogOn(new SteamUser.LogOnDetails
  123. {
  124. Username = user,
  125. Password = pass,
  126. });
  127. }
  128.  
  129. }
  130.  
  131. static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  132. {
  133. Console.WriteLine("Disconnected from Steam, reconnecting in 5...");
  134.  
  135. Thread.Sleep(TimeSpan.FromSeconds(5));
  136.  
  137. steamClient.Connect();
  138. }
  139.  
  140. static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  141. {
  142. if(steamAuth)
  143. {
  144. bool isSteamGuard = callback.Result == EResult.AccountLogonDenied;
  145. bool is2FA = callback.Result == EResult.AccountLoginDeniedNeedTwoFactor;
  146.  
  147. if (isSteamGuard || is2FA)
  148. {
  149. Console.WriteLine("This account is SteamGuard protected!");
  150.  
  151. if (is2FA)
  152. {
  153. Console.Write("Please enter your 2 factor auth code from your authenticator app: ");
  154. twoFactorAuth = Console.ReadLine();
  155. }
  156. else
  157. {
  158. Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
  159. authCode = Console.ReadLine();
  160. }
  161.  
  162. return;
  163. }
  164.  
  165.  
  166. }
  167. else
  168. {
  169. if (callback.Result != EResult.OK)
  170. {
  171. if (callback.Result == EResult.AccountLogonDenied)
  172. {
  173. // if we recieve AccountLogonDenied or one of it's flavors (AccountLogonDeniedNoMailSent, etc)
  174. // then the account we're logging into is SteamGuard protected
  175. // see sample 5 for how SteamGuard can be handled
  176.  
  177. Console.WriteLine("Unable to logon to Steam: This account is SteamGuard protected.");
  178.  
  179. isRunning = false;
  180. return;
  181. }
  182. }
  183. }
  184.  
  185. if (callback.Result != EResult.OK)
  186. {
  187. Console.WriteLine("Unable to logon to Steam: {0} / {1}", callback.Result, callback.ExtendedResult);
  188.  
  189. isRunning = false;
  190. return;
  191. }
  192. Console.WriteLine("Successfully logged on!");
  193.  
  194. // at this point, we'd be able to perform actions on Steam
  195. }
  196.  
  197. static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  198. {
  199. // before being able to interact with friends, you must wait for the account info callback
  200. // this callback is posted shortly after a successful logon
  201.  
  202. // at this point, we can go online on friends, so lets do that
  203. steamFriends.SetPersonaState(EPersonaState.Online);
  204. }
  205.  
  206. static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  207. {
  208. // at this point, the client has received it's friends list
  209.  
  210. int friendCount = steamFriends.GetFriendCount();
  211.  
  212. Console.WriteLine("We have {0} friends", friendCount);
  213.  
  214. for (int x = 0; x < friendCount; x++)
  215. {
  216. // steamids identify objects that exist on the steam network, such as friends, as an example
  217. SteamID steamIdFriend = steamFriends.GetFriendByIndex(x);
  218. // we'll just display the STEAM_ rendered version
  219. Console.WriteLine("Friend: {0}", steamIdFriend.Render());
  220. }
  221.  
  222. // we can also iterate over our friendslist to accept or decline any pending invites
  223. bool hasErrored = false;
  224. try
  225. {
  226. foreach (var friend in callback.FriendList)
  227. {
  228. if (friend.Relationship == EFriendRelationship.Friend)
  229. {
  230. steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, message);
  231. }
  232. }
  233. }
  234. catch(Exception e)
  235. {
  236. hasErrored = true;
  237. }
  238.  
  239. if(!hasErrored)
  240. Console.WriteLine("Messaged all friends!");
  241. }
  242.  
  243.  
  244. static void OnFriendAdded(SteamFriends.FriendAddedCallback callback)
  245. {
  246. // someone accepted our friend request, or we accepted one
  247. Console.WriteLine("{0} is now a friend", callback.PersonaName);
  248. }
  249.  
  250. static void OnPersonaState(SteamFriends.PersonaStateCallback callback)
  251. {
  252. // this callback is received when the persona state (friend information) of a friend changes
  253.  
  254. // for this sample we'll simply display the names of the friends
  255. Console.WriteLine("State change: {0}", callback.Name);
  256. }
  257.  
  258. static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  259. {
  260. Console.WriteLine("Logged off of Steam: {0}", callback.Result);
  261. }
  262.  
  263. static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
  264. {
  265. Console.WriteLine("Updating sentryfile...");
  266.  
  267. // write out our sentry file
  268. // ideally we'd want to write to the filename specified in the callback
  269. // but then this sample would require more code to find the correct sentry file to read during logon
  270. // for the sake of simplicity, we'll just use "sentry.bin"
  271.  
  272. int fileSize;
  273. byte[] sentryHash;
  274. using (var fs = File.Open("sentry.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
  275. {
  276. fs.Seek(callback.Offset, SeekOrigin.Begin);
  277. fs.Write(callback.Data, 0, callback.BytesToWrite);
  278. fileSize = (int)fs.Length;
  279.  
  280. fs.Seek(0, SeekOrigin.Begin);
  281. using (var sha = new SHA1CryptoServiceProvider())
  282. {
  283. sentryHash = sha.ComputeHash(fs);
  284. }
  285. }
  286.  
  287. // inform the steam servers that we're accepting this sentry file
  288. steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  289. {
  290. JobID = callback.JobID,
  291.  
  292. FileName = callback.FileName,
  293.  
  294. BytesWritten = callback.BytesToWrite,
  295. FileSize = fileSize,
  296. Offset = callback.Offset,
  297.  
  298. Result = EResult.OK,
  299. LastError = 0,
  300.  
  301. OneTimePassword = callback.OneTimePassword,
  302.  
  303. SentryFileHash = sentryHash,
  304. });
  305.  
  306. Console.WriteLine("Done!");
  307. }
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement