main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "libpsiphontunnel.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 *psiphon_config = read_file(config);
  29. if (!psiphon_config) {
  30. printf("Could not find config file: %s\n", config);
  31. return 1;
  32. }
  33. // set server list
  34. char *serverList = "";
  35. // set client platform
  36. char * const os = "OSName"; // "Android", "iOS", "Windows", etc.
  37. char * const os_version = "OSVersion"; // "4.0.4", "10.3", "10.0.10240", etc.
  38. char * const bundle_identifier = "com.example.exampleClientLibraryApp";
  39. char * client_platform = (char *)malloc(sizeof(char) * (strlen(os) + strlen(os_version) + strlen(bundle_identifier) + 4)); // 4 for 3 underscores and null terminating character
  40. int n = sprintf(client_platform, "%s_%s_%s", os, os_version, bundle_identifier);
  41. // set network ID
  42. char * const network_id = "TEST";
  43. // set timout
  44. long long *timeout = (long long*)malloc(sizeof(long long));
  45. *timeout = (long long)60;
  46. // connect 5 times
  47. for (int i = 0; i < 5; i++) {
  48. // start will return once Psiphon connects or does not connect for timeout seconds
  49. char *result = psiphon_tunnel_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. psiphon_tunnel_stop();
  54. }
  55. }