@@ -17,44 +17,6 @@ public static function run_task(string $functionName, array $args = array()): vo
1717 }
1818 }
1919
20- /**
21- * @param array<mixed> $content
22- */
23- private static function send_usage (string $ url , array $ content ): int
24- {
25- if (empty ($ url )) {
26- throw new \InvalidArgumentException ("URL cannot be empty " );
27- }
28-
29- $ body = json_encode ($ content );
30-
31- if ( ! $ body ) {
32- throw new \InvalidArgumentException ("Failed to encode JSON " );
33- }
34-
35- $ token = self ::download_url ($ url . '?connect ' );
36-
37- $ c = curl_init ();
38- curl_setopt ($ c , CURLOPT_URL , $ url );
39-
40- $ headers = [
41- 'Content-Type: application/json ' ,
42- 'Authorization: Bearer ' . $ token
43- ];
44-
45- curl_setopt ($ c , CURLOPT_POST , true );
46- curl_setopt ($ c , CURLOPT_POSTFIELDS , $ body );
47- curl_setopt ($ c , CURLOPT_HTTPHEADER , $ headers );
48- curl_setopt ($ c , CURLOPT_USERAGENT , 'plugin-metrics/1.0.0 ' );
49-
50- curl_exec ($ c );
51- if ( ! curl_errno ($ c )) {
52- $ info = curl_getinfo ($ c );
53- return $ info ['http_code ' ];
54- }
55- return -1 ;
56- }
57-
5820 public static function setPHPDebug (): void
5921 {
6022 $ version = parse_ini_file ('/var/local/emhttp/plugins/tailscale/tailscale.ini ' );
@@ -70,88 +32,6 @@ public static function setPHPDebug(): void
7032 }
7133 }
7234
73- public static function sendUsageData (Config $ config ): void
74- {
75- $ endpoint = "https://plugin-usage.edacerton.win/ " ;
76-
77- if ($ config ->Usage ) {
78- $ var = parse_ini_file ('/usr/local/emhttp/state/var.ini ' );
79- $ version = parse_ini_file ('/var/local/emhttp/plugins/tailscale/tailscale.ini ' );
80-
81- if ( ! $ var || ! $ version ) {
82- Utils::logmsg ("Could not retrieve system data, skipping usage data. " );
83- return ;
84- }
85-
86- $ tailscaleInfo = new Info (new Translator ());
87-
88- $ prefs = $ tailscaleInfo ->getPrefs ();
89- if (isset ($ prefs ->LoggedOut ) ? ($ prefs ->LoggedOut ? true : false ) : true ) {
90- Utils::logmsg ("Skipping usage data collection; not logged in. " );
91- return ;
92- }
93-
94- $ customControl = false ;
95-
96- if ($ prefs ->ControlURL != "https://controlplane.tailscale.com " ) {
97- $ customControl = true ;
98- }
99-
100- $ content = array (
101- 'clientId ' => hash ("crc32b " , $ var ['flashGUID ' ]),
102- 'plugin ' => 'tailscale ' ,
103- 'plugin_version ' => $ version ['VERSION ' ],
104- 'plugin_branch ' => $ version ['BRANCH ' ],
105- 'unraid_version ' => $ var ['version ' ],
106- 'bool1 ' => $ tailscaleInfo ->acceptsDNS (),
107- 'bool2 ' => $ tailscaleInfo ->acceptsRoutes (),
108- 'bool3 ' => $ config ->IncludeInterface ,
109- 'bool4 ' => (bool ) $ tailscaleInfo ->getAdvertisedRoutes (),
110- 'bool5 ' => $ tailscaleInfo ->advertisesExitNode (),
111- 'num1 ' => $ customControl ? 0 : 1
112- );
113-
114- $ attempts = 0 ;
115- $ delay = rand (0 , 300 );
116- do {
117- Utils::logmsg ("Waiting for {$ delay } seconds before sending usage data. " );
118- sleep ($ delay );
119- $ delay += 300 ;
120- $ attempts ++;
121-
122- $ result = self ::send_usage ($ endpoint , $ content );
123- Utils::logmsg ("Usage data sent. " );
124- } while (($ result != '200 ' ) && ($ attempts < 3 ));
125-
126- if ($ result != '200 ' ) {
127- Utils::logmsg ("Error occurred while transmitting usage data. " );
128- }
129- } else {
130- Utils::logmsg ("Usage collection disabled. " );
131- }
132- }
133-
134- public static function download_url (string $ url ): string
135- {
136- if (empty ($ url )) {
137- throw new \InvalidArgumentException ("URL cannot be empty " );
138- }
139-
140- $ ch = curl_init ();
141- curl_setopt ($ ch , CURLOPT_URL , $ url );
142- curl_setopt ($ ch , CURLOPT_FRESH_CONNECT , true );
143- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
144- curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 15 );
145- curl_setopt ($ ch , CURLOPT_TIMEOUT , 45 );
146- curl_setopt ($ ch , CURLOPT_ENCODING , "" );
147- curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
148- curl_setopt ($ ch , CURLOPT_FAILONERROR , true );
149- curl_setopt ($ ch , CURLOPT_USERAGENT , 'plugin-metrics/1.0.0 ' );
150- $ out = curl_exec ($ ch ) ?: false ;
151- curl_close ($ ch );
152- return strval ($ out );
153- }
154-
15535 public static function printRow (string $ title , string $ value ): string
15636 {
15737 return "<tr><td> {$ title }</td><td> {$ value }</td></tr> " . PHP_EOL ;
0 commit comments