main.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "PsiphonTunnel.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. char *read_file(char *filename) {
  6. char *buffer = NULL;
  7. size_t size = 0;
  8. FILE *fp = fopen(filename, "r");
  9. if (!fp) {
  10. return NULL;
  11. }
  12. fseek(fp, 0, SEEK_END);
  13. size = ftell(fp);
  14. rewind(fp);
  15. buffer = malloc((size + 1) * sizeof(*buffer));
  16. fread(buffer, size, 1, fp);
  17. buffer[size] = '\0';
  18. return buffer;
  19. }
  20. int main(int argc, char *argv[]) {
  21. // load config
  22. char * const default_config = "psiphon_config";
  23. char * config = argv[1];
  24. if (!config) {
  25. config = default_config;
  26. printf("Using default config file: %s\n", default_config);
  27. }
  28. char *file_contents = read_file(config);
  29. if (!file_contents) {
  30. printf("Could not find config file: %s\n", config);
  31. return 1;
  32. }
  33. GoString psiphon_config = {file_contents, strlen(file_contents)};
  34. // set server list
  35. GoString serverList = {};
  36. // set client platform
  37. char * const os = "OSName"; // "Android", "iOS", "Windows", etc.
  38. char * const os_version = "OSVersion"; // "4.0.4", "10.3", "10.0.10240", etc.
  39. char * const bundle_identifier = "com.example.exampleClientLibraryApp";
  40. char * test_client_platform = (char *)malloc(sizeof(char) * (strlen(os) + strlen(os_version) + strlen(bundle_identifier) + 4)); // 4 for 3 underscores and null terminating character
  41. int n = sprintf(test_client_platform, "%s_%s_%s", os, os_version, bundle_identifier);
  42. GoString client_platform = {test_client_platform, n};
  43. // set network ID
  44. char * const test_network_id = "TEST";
  45. GoString network_id = {test_network_id, strlen(test_network_id)};
  46. // set timout
  47. long long timeout = 60;
  48. // start will return once Psiphon connects or does not connect for timeout seconds
  49. char *result = Start(psiphon_config, serverList, client_platform, network_id, timeout);
  50. // print results
  51. printf("Result: %s\n", result);
  52. // The underlying memory of `result` is managed by PsiphonTunnel and is freed in Stop
  53. Stop();
  54. }