11using System ;
2- using System . Collections . Generic ;
3- using System . Net . Http ;
4- using System . Net . Http . Headers ;
5- using System . Threading . Tasks ;
62
73namespace AssistantComputerControl {
84 class AnalyticsSettings {
9- const string sendDataUrl = "https://acc.albe.pw/api/ReceiveAnalyticsData.php" ;
10- const string sendSharingUrl = "https://acc.albe.pw/api/IsSharing.php" ;
11- public const string sentryToken = "https://[email protected] /1287269" ; 12- private static readonly HttpClient client = new HttpClient ( ) ;
5+ //ACC truly is open source, but for security reasons we will not share this one file with the whole world.
6+ //This file handles sending analytics to the developers' webserver and contains a few variables that contain -
7+ //sensitive access-tokens to integrations like Sentry.IO
8+
9+ //The public version of this file is exactly like the full version, but stripped of -
10+ //API-keys and sensitive information.
11+ //Cencored items have placeholders to ensure that everyone can fork and run the project without errors
12+ public const string sentryToken = "super_secret" ;
1313
1414 public static readonly string [ ] actions = new String [ ] { //No changing this order!
1515 "shutdown" , //0
@@ -43,31 +43,13 @@ class AnalyticsSettings {
4343 "unknown" ,
4444 } ;
4545
46- public static void UpdateSharing ( bool doShare ) {
47- Properties . Settings . Default . SendAnonymousAnalytics = doShare ;
48- Properties . Settings . Default . Save ( ) ;
49-
50- //Notify server whether
51- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , @"jmF}AXK6NH!#{@/:ZHV#qK6r#YxytM>K/W6#5q/}tEK!&*_Gd\_YNUBNN/$a+$r" ) ;
52-
53- var parameters = new Dictionary < string , string > {
54- [ "uid" ] = Properties . Settings . Default . UID ,
55- [ "share" ] = doShare ? "true" : "false"
56- } ;
57-
58- try {
59- var response = client . PostAsync ( sendSharingUrl , new FormUrlEncodedContent ( parameters ) ) . Result ;
60- var contents = response . Content . ReadAsStringAsync ( ) . Result ;
61- MainProgram . DoDebug ( "Posted user response" ) ;
62- } catch ( Exception e ) {
63- MainProgram . DoDebug ( "Failed to send analytics data - something wrong with the server?" ) ;
64- MainProgram . DoDebug ( e . ToString ( ) ) ;
65- }
66-
67- SetupAnalytics ( ) ;
46+ class KeyHandler {
47+ public bool Status { get ; set ; }
48+ public string Message { get ; set ; }
49+ public string Key { get ; set ; }
6850 }
6951
70- public static void SetupAnalytics ( ) {
52+ public static void SetupAnalyticsAsync ( ) {
7153 //Unique user-ID
7254 if ( Properties . Settings . Default . UID == "" || Properties . Settings . Default . UID == null ) {
7355 string newUID = Guid . NewGuid ( ) . ToString ( "N" ) ;
@@ -113,22 +95,11 @@ public static void SetupAnalytics() {
11395 Properties . Settings . Default . Save ( ) ;
11496 }
11597
116- DateTime dateTime = DateTime . UtcNow . Date ;
117- string thisDay = dateTime . ToString ( "yyyy/MM/dd" ) ;
118- if ( Properties . Settings . Default . AnalyticsUnsentData && Properties . Settings . Default . AnalyticsThisDay == thisDay ) {
119- //Resume data for today
120- ScheduleAnalyticsSend ( ) ;
121- } else if ( Properties . Settings . Default . AnalyticsUnsentData && Properties . Settings . Default . AnalyticsThisDay != thisDay ) {
122- //Didn't send last time - sending now
123- SendAnalyticsData ( ) ;
124- }
125-
12698 MainProgram . DoDebug ( "Annonymous analytics setup done" ) ;
12799 } else {
128100 MainProgram . DoDebug ( "Annonymous analytics are not being shared" ) ;
129101 }
130102 }
131-
132103 public static void PrintAnalytics ( ) {
133104 int i = 0
134105 , totalCount = 0 ;
@@ -149,30 +120,18 @@ public static void AddCount(string action, string type) {
149120 int pos = Array . IndexOf ( actions , action ) ;
150121 if ( pos > - 1 ) {
151122 //MainProgram.DoDebug("Added +1 to " + action + " at pos " + pos);
152- if ( Properties . Settings . Default . TotalActionsExecuted . Length >= pos ) {
153- Properties . Settings . Default . TotalActionsExecuted [ pos ] ++ ;
154-
155- if ( ! Properties . Settings . Default . AnalyticsUnsentData ) {
156- Properties . Settings . Default . AnalyticsUnsentData = true ;
157- ScheduleAnalyticsSend ( ) ;
158- }
159- Properties . Settings . Default . Save ( ) ;
160- } else {
161- MainProgram . DoDebug ( "Index " + pos . ToString ( ) + " exceeds 'TotalActionsExecuted' property, which has length; " + Properties . Settings . Default . TotalActionsExecuted . Length . ToString ( ) ) ;
162- }
123+ Properties . Settings . Default . TotalActionsExecuted [ pos ] ++ ;
124+ Properties . Settings . Default . Save ( ) ;
163125 } else {
164126 MainProgram . DoDebug ( "Could not find action \" " + action + "\" in action-array (analytics)" ) ;
165127 }
128+
129+ SendAnalyticsData ( ) ;
166130 }
167131 public static void AddCount ( int action , string type ) {
168132 AddTypeCount ( type ) ;
169133 if ( actions [ action ] != null ) {
170134 Properties . Settings . Default . TotalActionsExecuted [ action ] ++ ;
171-
172- if ( ! Properties . Settings . Default . AnalyticsUnsentData ) {
173- Properties . Settings . Default . AnalyticsUnsentData = true ;
174- ScheduleAnalyticsSend ( ) ;
175- }
176135 Properties . Settings . Default . Save ( ) ;
177136 } else {
178137 MainProgram . DoDebug ( "Could not find action with index \" " + action + "\" in action-array (analytics)" ) ;
@@ -194,67 +153,14 @@ private static void AddTypeCount(string type) {
194153 }
195154 }
196155
197- static void ScheduleAnalyticsSend ( ) {
198- //Time when method needs to be called
199- MainProgram . DoDebug ( "Analytics scheduled to be sent at 00:00" ) ;
200-
201- DateTime dateTime = DateTime . UtcNow . Date ;
202- Properties . Settings . Default . AnalyticsThisDay = dateTime . ToString ( "yyyy/MM/dd" ) ;
203- Properties . Settings . Default . Save ( ) ;
204-
205- var DailyTime = "00:00:00" ;
206- var timeParts = DailyTime . Split ( new char [ 1 ] { ':' } ) ;
207-
208- var dateNow = DateTime . Now ;
209- var date = new DateTime ( dateNow . Year , dateNow . Month , dateNow . Day ,
210- int . Parse ( timeParts [ 0 ] ) , int . Parse ( timeParts [ 1 ] ) , int . Parse ( timeParts [ 2 ] ) ) ;
211- TimeSpan ts ;
212- if ( date > dateNow )
213- ts = date - dateNow ;
214- else {
215- date = date . AddDays ( 1 ) ;
216- ts = date - dateNow ;
217- }
218-
219- Task . Delay ( ts ) . ContinueWith ( ( x ) => SendAnalyticsData ( ) ) ;
220- }
221-
222156 public static string SendAnalyticsData ( ) {
157+ //Sends analytics data to the server
223158 if ( Properties . Settings . Default . SendAnonymousAnalytics ) {
224- if ( MainProgram . HasInternet ( ) ) {
225- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , @"jmF}AXK6NH!#{@/:ZHV#qK6r#YxytM>K/W6#5q/}tEK!&*_Gd\_YNUBNN/$a+$r" ) ;
226-
227- var parameters = new Dictionary < string , string > {
228- [ "uid" ] = Properties . Settings . Default . UID ,
229- [ "actions" ] = string . Join ( "," , Properties . Settings . Default . TotalActionsExecuted ) ,
230- [ "assistants" ] = string . Join ( "," , Properties . Settings . Default . AssistantType ) ,
231- [ "start_with_windows" ] = Properties . Settings . Default . StartWithWindows ? "true" : "false" ,
232- [ "version" ] = MainProgram . softwareVersion ,
233- [ "beta_program" ] = Properties . Settings . Default . BetaProgram ? "true" : "false" ,
234- [ "date" ] = Properties . Settings . Default . AnalyticsThisDay
235- } ;
236-
237- try {
238- var response = client . PostAsync ( sendDataUrl , new FormUrlEncodedContent ( parameters ) ) . Result ;
239- var contents = response . Content . ReadAsStringAsync ( ) . Result ;
240-
241- //Success
242- Properties . Settings . Default . AnalyticsUnsentData = false ;
243- Properties . Settings . Default . Save ( ) ;
244- MainProgram . DoDebug ( "Analytics successfully posted" ) ;
245- return contents ;
246- } catch ( Exception e ) {
247- MainProgram . DoDebug ( "Failed to send analytics data - something wrong with the server?" ) ;
248- MainProgram . DoDebug ( e . ToString ( ) ) ;
249- return "" ;
250- }
251- } else {
252- MainProgram . DoDebug ( "Failed to send analytics data; no internet connection" ) ;
253- return "" ;
254- }
159+ //Do it (sensitive code)
255160 } else {
256- return "" ;
161+ //Don't do it (does nothing here)
257162 }
163+ return "" ;
258164 }
259165 }
260166}
0 commit comments