Advertisement
Guest User

Message all steam friends - Steam Authentication

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